Closed github-learning-lab[bot] closed 2 years ago
Great job! Now that we know how to handle outputs and inputs, in the next steps we are going to build a new Ruby program and learn how to use If/Else statements to determine which code to run depending on the results of our inputs.
Click here for the last steps!
Prompting Us for Input
Knowing how to print strings is great, but it can get stale printing the same output over and over again. The next concept we are going to learn is how to make our program address us by name! We can do that by prompting for user input and then using that input to return an output with the unique input.
Let's start by heading back into our hello.rb file and we'll change our output from saying "Hello World!" to a question asking our name. Modify line three to the following:
We are now using the
puts
method to output a question asking us our name. Below that we have our second lesson which will allow us to enter our name and capture our input. Uncomment the code on line seven so that it looks like this:The seventh line introduces a new method,
gets
, and a variable calledname
.What the
gets
method does is tell the computer to pause for input from the user. Our program will pause to allow us to enter in any text we want, and when we're ready to resume the program, we can press the ENTER key. Our inputs are then captured and converted to a string.Variables are a way to store information. Similar to how methods are used to store code that can later be used by calling the method, variables store information like strings that can later be accessed by calling the variable. For example, if I assign the string
Octocat
to the variablename
, I could then callputs name
and the output would be the stringOctocat
.We put this to use in our eighth line of code. Uncomment the eighth line and your project should have the following code:
This new line introduces another new Ruby feature called string interpolation. String interpolation is used to take the content within a variable and insert it into a string. For example, the
puts
method above will output the value within the variablename
rather than the word name itself.We can see this in action by saving our file and running our Ruby file in the command line again. Our program should now ask us our name. We will then be able to enter our name and after entering our name and pressing the ENTER key, we will see a new output with our name in that output.
It may not look how we expect because of an unexpected line break after our name. That's because the
gets
method captures all the keystrokes including the ENTER key. The ENTER key inside a string creates a newline character, and that newline character is what creates the line break. There is a way we can fix this though by using a method calledchop
on the captured string.The
chop
method takes a string and removes the very last character of that string, in this case, the newline character.Let's head back into our hello.rb file and add the
chop
method after ourgets
method. Modify the seventh line so that it looks like this:Save the file and now if we run our program in the terminal we should see that the line break is no longer. Our output now looks as we expect it to and we've successfully created a Ruby program that outputs strings, prompts us for input, and outputs new strings using our inputs.
Your hello.rb file should look like this:
Before moving forward to the next section, let's commit and push our changes to the repo following the steps outlined above.