Locked lesson.
About this lesson
Allowing the user to interact with your program.
Exercise files
Download this lesson’s related exercise files.
Getting User Input with Gets.docx60.1 KB Getting User Input with Gets - Solution.docx
60.1 KB
Quick reference
Getting User Input with Gets
How to get user input.
When to use
Whenever you want to receive input from a user, use gets.
Instructions
Gets stands for Get String.
To get user input use gets:
user_input = gets.chomp
To convert user input to an integer, use to_i
user_input = gets.to_i
To convert user input to a float, use to_f
user_input = gets.to_f
Hints & tips
- Gets lets us Get input from a user
- to_i stands for To Integer
- to_f stands for To Float
- #{ } allows us to do Ruby code inside a string
- 00:04 In this video, I want to talk about getting user input.
- 00:07 And up until now, our programs have just run and output stuff to the screen.
- 00:11 But there hasn't been any real user interaction.
- 00:13 So if we want to ask our user something, get input from them,
- 00:17 have them type in something, we need to be able to do that.
- 00:20 So to do that, we use something called gets, and that allows us to get input.
- 00:24 So let's put something on the screen.
- 00:27 Let's go, puts "Enter Your Name".
- 00:32 Actually, let's do print so that it does it all on one line.
- 00:37 And let's create a variable called, well, let's just call it name, name = gets.
- 00:42 So if we save this and run it, we see Enter Your Name.
- 00:46 And if we type, John Elder, and hit Enter, the program ends.
- 00:49 Now we haven't told the program to do anything with this name,
- 00:53 so the program just ends.
- 00:55 But whatever the user types in gets put into this variable called name.
- 00:59 So gets stands for get s.
- 01:02 And the s stands for string, so get string.
- 01:05 We've created a string and we slapped it in this name variable.
- 01:08 So we could say, puts "Hello", and
- 01:12 remember we can do math, so we can add that out.
- 01:15 So now if we run it, Enter Your Name, John.
- 01:18 Hello, John, and that's kind of fun.
- 01:20 Let's run this again.
- 01:21 So Enter Your Name, 12.
- 01:23 Hello, 12, right?
- 01:25 What if I come down here and go, puts name + 1?
- 01:29 So if I type in 12, what do you think's going to happen?
- 01:32 Are we going to get 13?
- 01:33 No, we're going to get an error, because gets stands for get string, and
- 01:37 a number is not a string, a number is a number.
- 01:40 So you can't add a string and a number, and you get an error.
- 01:44 But we can change this with a Ruby method, the to_i method.
- 01:47 All I have to do is come up here and put gets.to_ i, and
- 01:51 that changes it to an integer.
- 01:53 If we wanted to change it to a float, we can do to_f.
- 01:57 Just a little bit of interesting trivia.
- 01:59 If we don't want to add a string and an integer, so
- 02:02 I'll comment this line out, if we run it again, 13 gets.to_i.
- 02:06 One more thing, I want to take this off, I want to mention is right here.
- 02:12 We've done this "Hello" + name.
- 02:14 Now, you can absolutely do that, but sometimes you don't want to.
- 02:17 Sometimes you do it all in your string.
- 02:19 And to do that, we can use something called interpolation.
- 02:22 It's just a fancy computer science term.
- 02:23 If we go, #, or number sign, and then {}.
- 02:28 Now anything inside of this, we can execute as Ruby code.
- 02:32 So we can type in our variable, save it, run the program, Enter Your Name, John.
- 02:38 Hello, John.
- 02:39 And it will execute it without having to put this + name at the end.
- 02:43 Just sort of a more elegant way to do it.
- 02:46 And we'll use this interpolation method an awful lot.
- 02:49 I prefer to do it that way for lots of different reasons.
- 02:52 For one thing, if we save this and run it, it will output an integer.
- 02:56 That's how you get user input, very easy.
Lesson notes are only available for subscribers.