Difference between put and println in Ruby with examples

As you know print and puts are used to display data to console.

put and print are functions defined in io class in Ruby. These objects print the arguments to console and return nil What is difference between puts and print, when to use print and puts?

Difference between put and print

puts and prints function used to print an object to console.

In case of Singe arguments to this two functions, it adds the same output.

puts "hello world"
print "hello world"

Output:

hello world
hello world

puts adds new line after output string to console. print does not add line after output string to console

Let’s see an example of multiple arguments. You can add multiple arguments to these using comma separated.

puts "hello","world"
print "hello", "world"

In this, puts an new line for every arguments and does not end with new line.

print output argument to console and does not add output. Output:

hello
world
helloworld

Let’s print an array in Ruby with examples.

print [11,nil, 121, 122]
puts [11,nil, 121, 122]

Output:

[11, nil, 121, 122]11

121
122