Signup/Sign In

Display Data using print() and puts()

Now, we will learn how to display data to the screen. For this purpose, Ruby provides two functions :

  • puts()
  • print()

Ruby: puts() method

puts adds a new line automatically at the end of the data.

puts ("hello, world")
puts "hello, world"

You can use both the ways to display the data. To print multiple strings in a single puts statement use the below approach. It will add a newline character at the end of each string.

puts "hello, world", "Goodbye, world"

Using puts method in Ruby

Also try the below example :

puts "practice", "makes", "a", "man", "perfect"

Using puts method in Ruby


Ruby: print() method

print doesn't add a new line at the end.

print "hello, world"

If you want to include new line at the end of the data using print method, then you have to include special character \n for new line.

print "hello, world\n"
print "practice\n", "makes\n", "a\n", "man\n", "perfect\n"

Using print method in Ruby

You can also display the numeric data using these functions.

puts(1)
print(1)

Also you can evaluate the expression and display the result.

puts (2+2)
print(2+2)

Using print method in Ruby