Getting input from User in Ruby language
Sometimes during the program you want ask the user to interact with the program, like asking their name or other details.
This can be achieved by using the Ruby gets
method which may be considered the opposite of puts
which will print the output to standard output device, i.e., the terminal.
In Ruby, we can get user input like this:
puts "Enter your name: "
name = gets.chomp
puts "Hello #{name}, how are you?"
Running this code, I would see this:
Enter your name:
RubyGuru
Hello, RubyGuru, how are you?
The reason for using `chomp` after `gets` is that `gets` will read the data entered by user and store into variable `name` along with new line character that you gave with the press of the enter or return key, represented by a 'new line' character as `\n`. The method `chomp` will remove the trailing new line character and store the rest of the data into `name`. |
puts "Enter your name: "
name = gets
puts "Hello #{name}, how are you?"
Without the chomp
, it will show the “enter” at the place that I pressed it.
I will run the code above, and we can see:
Enter your name:
RubyGuru
Hello, RubyGuru
, how are you?
Extended example
In the following example, I have extended the use of gets
to accept other details of person and transforming the data into appropriate variables.
print "Enter your name: "
name = gets.chomp
print "Enter your age: "
age = gets.to_i
print "Enter your address: "
address = gets.chomp
puts "Hello, #{name}, how are you?"
puts "If I am right, your age is '#{age}'."
puts "And, your address is '#{address}', right?"
When I run the above program:
Enter your name: foo
Enter your age: 10
Enter your address: barpak, gorkha, Nepal
Hello, foo, how are you?
If I am right, your age is '10'.
And, your address is 'barpak, gorkha, Nepal', right?
NOTE
to_i
is used to convert the numeric string toInteger
.print
is used to ask for the information becauseputs
will add new line character and give a feeling that you are entering your data on another line.
Calculator
Let’s build a small calculator program in Ruby. Here, we ask the user for two numbers and then print the result of addition of them.
print "Enter first number: "
number1 = gets.to_f
print "Enter second number: "
number2 = gets.to_f
puts "#{number1} + #{number2} = #{number1 + number2}"
Output
Enter first number: 5
Enter second number: 15
5.0 + 15.0 = 20.0
Help me to improve Gorkha Dev.