1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Getting and Printing Strings


One of the first interfaces you'll learn is the "get and print" interface. Printing strings to the terminal and reading strings from keyboard input. While this might not be all that useful later on, when you're learning, debugging programs or writing scripts, this is a most useful paradigm.

In the Ruby world, reading a string is referred to as "getting" and printing a string is referred to as "putting." To read a string form the terminal, you use the gets or "get string" method. Similarly, to print a string to the terminal, you use the puts or "put string" method.

Printing Strings with 'puts'

The puts method typically takes a single string argument. It will print this string to the terminal followed by a newline. Note that no newline is embedded in the string, puts takes care of that for you.

puts "Hello, world!"

If the object you pass to puts is not a string, the puts method will attempt to call to_s on that object. For example, if I were to pass a number to puts, it will first call to_s which, on a number, will return the string representation of that number. On other objects, it may return something a bit less than useful, but it will make an effort to turn it into something human-readable.

puts "A string"
puts 23
puts 10..20
puts Time.now
puts :symbol
puts Proc.new{}

This will produce the following output.

A string
2011-08-17 02:31:57 -0400

Note that it successfully turned every object passed to it into a reasonable string except the Proc object. What exactly does it mean to turn a Proc object into a string? It doesn't make much sense, to Proc#to_s simply returns the same as Proc#inspect, which is a general description of the object containing its type and memory address.

Printing Multiple Strings

While puts typically takes a single argument, it can also take multiple arguments. It will iterate over these arguments and print them one by one, with a newline after each of them.

puts "Hello", "world", 1337, 42

Alternatively, if an Array object is passed to puts, it will be iterated over just as an it iterated over the argument list.

puts [ 1, 2, 3, "A string", :symbol ]

If you wish to have more control over this, or simply want to make it clear that you're printing all elements of the array, you could always do something like this as well.

[1, 2, 3].each do|i|
  puts i

Printing an Empty Line

Calling puts without any arguments will print a new line and nothing more. This can be used to move the cursor down to the next line, or done a number of times to clear the screen.

Don't Use "print"

Almost every programming language has a method (or function) called "print." This print method will take a string and print it to the terminal. Ruby does have a print method, but it's not often used. Why not? Because the puts method prints a string and a newline character, so the cursor jumps down to the next line. Whereas the print method doesn't print the newline, if you want a newline you'll have to embed one in your string with the newline escape sequence \n.

This reflects a common idiom in Ruby where methods will do what you intend them to do. If you were to grep through all your C or Perl or Python code for all the print statements, you'd find that almost every single one of them embeds a newline at the end of the string. Ruby just saves you a step and has the puts method do this for you. You can use the print method, there's nothing wrong with it, but it's not idiomatic except in the situation explained in the gets section below.

  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Strings
  6. Getting and Printing Strings

©2014 About.com. All rights reserved.