How to Use The Ruby Gets & Chomp Methods

You’re writing a Ruby program & you want to ask the user a question…

How can you do that?

Well, you can use the Ruby gets method to read user input.

You can call gets.

Then your program starts waiting for you to type something with your keyboard & press the enter key.

The result?

You get back a string.

This string has the contents of what you (or the user) typed in, so if you assign this string to a variable you’ll be able to use it.

A common example is a greeting program.

It looks like this:

name = gets.chomp

puts "Hello #{name}, nice to meet you!"

The #{name} thing is called string interpolation

Give this a try & see for yourself how it works.

  • First, save this code into a “greeting.rb” file
  • Then, use it with “ruby greeting.rb” (from a terminal program)

Enter your name & your program will greet you!

Pretty cool.

Now:

What’s this chomp business all about?

It’s a Ruby method that changes the results of gets in a very specific way.

Try removing it.

Including the dot (.) before it, so it becomes name = gets, instead of name = gets.chomp.

If you save your code & run it again…

You’ll see that your greeting is now broken down into two lines, instead of the whole greeting being in one line.

What’s up with that?

Let’s dig into the details to find out!

Understanding Newlines & Special Characters

Ruby reads everything you type into the program while the get method is active.

For our greeting program, this includes your name.

And a special “new line” character which makes the greeting message break into two lines!

Meet the new line character:

\n

These kinds of special characters are normally invisible & show up only in the form of new lines, spaces, colors, etc.

If you want to see them you need a magic flashlight with a price of $10,000.

Just kidding!

All you need is the p method or the inspect method.

Example use:

p name
# "Jesus\n"

There it is!

Our “new line” character (\n).

This brings us back to our original question…

What’s chomp all about?

The purpose of chomp is to remove the newline character at the end of strings.

As you have seen, that’s very helpful!

Another Example

Having this newline character, or some other special character, affects more things than how a string looks when you print it.

It can also have an effect on your if statements!

Example:

name = gets

if name == "David"
  puts "Hello David, we were expecting you!"
end

In this code, if you don’t use chomp with gets, the two strings won’t be the same because of the newline character (\n), so this evaluates to false.

Btw…

This also applies to strings you get outside of gets, like the data you get from reading a database, a file, or any other source.

Keep an eye open for these special characters!

Chomp vs Chop vs Strip

Another two methods that you may find helpful for cleaning user input are chop & strip.

With strip you can remove white spaces.

Example:

"    John Smith    ".strip

With chop you’ll always remove the last character.

Removing The First Character

Another thing you may need is to remove characters from the beginning.

You can do it like this:

str = "Mr. John"

str[0..3] = ""

str
# "John"

This removes the first four characters.

Two Gets Methods? The Plot Thickens!

There is a special case…

Where you’ll need to use $stdin.gets instead of just gets.

If you get an error like this:

`gets': No such file or directory @ rb_sysopen

Why?

Because there are two version of the gets method.

  • The default version tries to read a file name
  • The alternative version ($stdin.gets) always reads from user input

Now you know, if you see an error like that while using gets, try changing it to $stdin.gets.

Summary

You’ve learned about the gets & chomp methods, what they do, how they work, and why they’re useful!

Keep learning with this Ruby tutorial.

Thanks for reading! 🙂