Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Input and Output in Ruby!
You have completed Practice Input and Output in Ruby!
Preview
Now that you've coded your solution to the practice problem, I'll show you how I did it.
Here's my code:
# Prompt the user to enter a verb, and store what they enter
# in a variable named "verb".
print "Please enter a verb: "
verb = gets.chomp
# Prompt the user to enter a noun, and store what they enter
# in a variable named "noun".
print "Please enter a noun: "
noun = gets.chomp
# Prompt the user to enter an adjective, and store what they
# enter in a variable named "adjective".
print "Please enter an adjective: "
adjective = gets.chomp
# Prompt the user to enter a second noun, and store what they
# enter in a variable named "noun2".
print "Please enter another noun: "
noun2 = gets.chomp
# Fill in the #{} marker to include the contents of the
# "verb" variable in the below string.
puts "One day, I decided to learn to #{verb} in Ruby."
# Use the contents of the "noun" variable in this sentence.
puts "So I turned on my #{noun} and logged in to Treehouse."
# Use the contents of the "adjective" variable in this sentence.
puts "Their teachers were really #{adjective}."
# Use the contents of the "noun2" variable in this sentence.
puts "In no time, I'd learned to program a simple #{noun2}!"
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Your goal was to create a simple word game
that prompted the user for verb, noun, and
0:00
so on, stored the answers and used those
answers to fill-in blanks in a story.
0:04
Here's my solution.
0:09
It's okay if yours is slightly different,
but if you see something interesting in my
0:11
code, you should consider borrowing
it to improve your own program.
0:14
So, first we needed to prompt
the user to enter a verb.
0:18
I called the print method to display
the prompt and passed a string to it.
0:22
The reason I used print instead of put
add, is that print doesn't add a new
0:25
line on to the end of whatever string
it prints, so that the prompt appears
0:30
right there on the same line
where the user types their entry.
0:35
So, let me try entering a verb here now.
0:39
So, you'll notice the program waited for
an entry, and
0:44
didn't proceed until I typed something.
0:46
It was this call to the gets
method that did that.
0:48
Gets stands for get string.
0:52
And basically,
it gets a string from the user.
0:54
Whatever the user types when it
prompts that's the return value from
0:57
the gets method.
1:01
We then assign that return
value to a variable
1:02
in this case we chose
the variable name verb.
1:05
So, down here I typed the word jump and
that means,
1:09
that now the string jump is
stored in the verb variable.
1:12
We repeat that process for
the other words that we need to collect.
1:18
So, we prompt for a noun.
1:21
We call gets to get the user's entry,
1:23
and then we store the return value
of gets in a variable named noun.
1:25
We prompt for
an adjective called gets, and
1:30
store the results in
a variable named adjective.
1:32
And finally, we prompt for
a noun called get s again.
1:35
And store this one in
a variable named noun2 to
1:39
distinguish it from the just
plain noun variable.
1:42
Now, we need to print out
our actual story, and
1:47
substitute in the values stored
in our variables into that story.
1:50
We provided you with several
pre-made sentences for your story.
1:55
In the form of calls to the puts method.
1:59
We added a string to each one, and we
provided blank interpolation markers here.
2:04
This here is a Ruby string
interpolation marker,
2:10
it starts with a hash mark, or pound sign.
2:14
An open curly brace and
a closed curly brace.
2:18
Whatever you put between the open and
2:20
closing curling braces here gets
substituted into the string.
2:23
Basically like filling in a blank.
2:27
So, this will take the value,
the string stored in the verb variable.
2:29
And it'll put it into
the middle of this sentence.
2:33
Then we do the same
with the noun variable,
2:37
the same with the adjective variable,
and the same with the noun two variable.
2:39
And when all of those run,
we have a completed story.
2:43
So, let's go back to the console here.
2:46
I'm gonna make it a little bigger,
so we can see everything.
2:48
And I'm going to finish filling
in the words it requests.
2:50
We're in the middle of
a call to the gets method.
2:54
So, I'm going to type in a string and
hit enter.
2:57
So, I'll use the noun desk.
3:01
Then I'll move to our next prompt,
and our next call to the gets method.
3:04
It'll ask me for an adjective.
3:08
I'll say, quick.
3:09
And finally it will last me for
a second, now, so I'll say, can.
3:12
And with those calls to gets complete,
it prints out our story.
3:17
One time I decided to learn to jump
in Ruby, so I turned on my desk and
3:20
logged in to Treehouse.
3:24
Their teachers were really quick, and in
no time I learned to program a simple can.
3:25
Doesn't make a lot of sense, but, hey,
that's the point of the game, right?
3:30
Now, you may notice that the formatting
of our story looks a little strange.
3:33
After each of the words it's been
filled in, it skips down to a new line.
3:37
We don't really want that.
3:40
The reason it skips to a new line is for
each call to the gets method,
3:42
we press Enter after filling in our entry.
3:47
And when we press Enter, that adds a new
line character onto the end of the string.
3:51
We need to remove that new line character,
so that our story is formatted properly.
3:55
To do that, I'm going to introduce you to
a new method that we can call directly on
3:59
the string that gets returned from get.
4:03
To do that, I'm going to introduce
you to a new method called chomp.
4:06
It's called chomp, because it eats a new
line character off the end of a string.
4:10
We called the chomp method directly on
the string value that we want to update.
4:15
And we call a method on a value
by using the dot operator.
4:20
So, I'm gonna take the return
value from gets here, and
4:23
I'm going to call a method directly on it.
4:27
So, I'll put a dot, a period right there.
4:29
And then I'll put the name of
the method that I want to call, chomp.
4:33
So, gets will return the value
that the user types,
4:39
along with a new line
character at the end.
4:41
And .chomp will call the chomp
method on that string.
4:44
That means,
chomp will return the same string, but
4:48
with the new line character removed.
4:51
And that's what will get stored here,
in the verb variable.
4:52
So, I'm gonna copy that call to .chomp,
and
4:56
I'm gonna add that to all
the other calls to gets.
4:58
So, that will remove all these
extra new line characters.
5:02
Save that, and
I'll go back down here to the console,
5:07
and I'm gonna re-run my program.
5:11
So, make the same entries before,
I'll enter verb, jump, a noun,
5:14
desk, an adjective,
quick, and another noun,
5:19
can, and you'll notice this time it's
removed all the new line characters
5:24
from the end of our entries, so jump
doesn't skip to a new line afterwards,
5:29
neither does desk, neither does quick,
and neither does can.
5:35
So, that's cleaned up
the formatting of our story a lot.
5:40
If you're not used to calling methods
directly on values, don't worry.
5:43
We'll be taking a closer look at
how to do that in later courses.
5:47
I hope,
you got in some good Ruby practice.
5:51
Feel free to add on to
your solution if you want.
5:53
For example, you might wanna prompt for
additional words and
5:55
add more to the pre-made story.
5:58
Have fun!
5:59
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up