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
Lesson notes are only available for subscribers.