Different between “gets” and “gets.chomp” in ruby


Hi friends,

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.

Ex:


# 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
we
=> “we\n”
ruby-1.9.2-p136 :002 > a = gets.chomp
we
=> “we”

That’a all !!!🙂

4 thoughts on “Different between “gets” and “gets.chomp” in ruby

  1. If some one desires expert view concerning blogging and site-building after that i suggest him/her to pay a
    quick visit this blog, Keep up the good job.

  2. That is very attention-grabbing, You’re an excessively professional blogger.
    I’ve joined your rss feed and look ahead to in the hunt for extra of your great post.
    Also, I have shared your site in my social networks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s