Close
Close

Taking Input in Ruby


Now, you have the knowledge of printing something on the screen. But in the real world, you develop programs for users. So, there should be some way to take inputs from the user. In Ruby, it is done with gets. Let's see an example first.

puts "What is your name?"
a = gets.chomp
puts "You name is #{a}"
Output
What is your name?
xyz
Your name is xyz

gets.chomp is used to take string input from users.

a = gets.chomp : gets.chomp takes input from the user through the keyboard and store it in the variable a.
So, if the user enters xyz, then you can think that now gets.chomp is "xyz".
So, a = gets.chomp will then be equivalent to a = "xyz".

Now, we know that gets.chomp takes an input from the user as string. So, even if we give 5 to gets.chomp, then also it will be "5" ( a string ) and not 5 ( a fixnum or integer ). Let's try this.

a = gets.chomp
puts "#{a}"
puts "#{a.class}"
Output
5
5
String

As you have seen that in the previous example, a.class gave us String because gets.chomp takes string inputs.

How to take integer inputs?


In Ruby, one can use get.chmop.to_i to take integer inputs from user.

a = gets.chomp.to_i
puts "#{a}"
puts "#{a.class}"
Output
5
5
Fixnum

In this example, the class of a is Fixnum and this shows that a is an integer. So, get.chomp.to_i took integer input from the user.
a = gets.chmop.to_i : If the user enters 5, then a = gets.chomp.to_i will be equivaltent to a = 5 ( not a = "5", as in case of gets.chomp ). Let's see one more example on this:

a = gets.chomp.to_i
b = gets.chomp.to_i
puts "You have entered #{a} and #{b} and their sum is #{a+b}"
Output
5
2
You have entered 5 and 2 and their sum is 7

Let's take float input


As gets.chomp.to_i is used for Fixnum, gets.chomp.to_f is used for Float. Let's see an example:

a = gets.chomp.to_f
b = gets.chomp.to_i
puts "You have entered #{a} and #{b} and their sum is #{a+b}"
puts "#{a.class}"
Output
5.265656
2
You have entered 5.265656 and 2 and their sum is 7.265656
Float

We can see that the class of a is Float and we used gets.chomp.to_f to take the float value.

Type conversion in Ruby


Ruby provides the simplest way to convert from one data type to another. We use to_i to convert a float into Fixnum ( or integer ). Also, we can use to_f to do the conversion to Float.
See the following example to understand this.

a = 2.34
b = a.to_i
c = 34
d = c.to_f
puts "#{a}\n#{b}\n#{d}"
puts "#{a.class}\n#{b.class}\n#{d.class}"
Output
2.34
2
3.0
Float
Fixnum
Float

As you have seen, a.to_i converted 2.34 to an integer 2, and c.to_f converted 34 to a float 34.0.

type conversion in Ruby

Let's do some maths


Ruby provides a number of functions to do maths in its Math module. To use any method of any module, we use dot (.) after the module name followed by that method name ( Module_name.method_name ). This will be clear by the following example :

#computes cosine
puts Math.cos(0)
#computes sine of the number
puts Math.sin(0)
#computes natural logrithm
puts Math.log(1)
#computes log of a base b in log(a,b)
puts Math.log(100,10)
#computes square root of given number
puts Math.sqrt(2)
#computes tangent of given number
puts Math.tan(0)
Output
1.0
0.0
0.0
2.0
1.4142135623730951
0.0

Here, we have used the Math module. sin, cos, log, sqrt, etc are methods inside this Math module. So, we have used dot (.) to use a specific method of Math module. E.g.- Math.sin, Math.cos, etc. There are many more methods in this Math module of Ruby. You can see them all in Ruby documentation.

Using programming, your maths is going to be fun!

If you are not aware of these mathematical functions, then don't go into these and continue your coding.

With every chapter, you are studying lots of new concepts. It is very necessary to solve problems.

The best way to predict the future is to create it.
-Peter Druker


Ask Yours
Post Yours
Doubt? Ask question