The main different between “gets” and “gets.chomp” in ruby is, when you get the input from user, using “gets” method the outputs are ‘ll come like “…/n”. i mean the output ‘ll come whatever you gave and at the same time a “/n” also ‘ll come next to the output, because of when we enter the “Enter” button the “gets” method also assume “Enter” is like as a new line input.
So only, we using the “gets.chomp” method. “chomp” means like “cut”. Because when we using “.chomp” method, it cuts the last parameter like “/n” from input.
# The simple program explains "what's the different between 'gets' and 'gets.chomp' method in ruby. =begin Program name: gets.chomp.rb Date written: 03.02.2011 Date modified: 03.02.2011 Author: Tha.Suresh Licence: GPL =end # Main part of the starts here #!/usr/bin/ruby puts "After entering something, you can see the the 'New Line': " a = gets print a puts "After entering something, you can't see the the 'New Line': " a = gets.chomp print a
Ex: Direct preview in “irb”
[suresh@mercury ~]$ irb
ruby-1.9.2-p136 :001 > a = gets
ruby-1.9.2-p136 :002 > a = gets.chomp
That’a all !!!🙂