{

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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to check a string contains a substring in Ruby with examples

How to check if element exists in hash or not in Ruby with examples

How to check if an element exists in Array or not in Ruby with examples

How to check if the variable is defined in Ruby with examples

How to check the type of a variable is Ruby| Ruby on Rails By Example

How to convert Class or Hash Object to JSON Object Ruby| Ruby on Rails By Example

How to Convert current Unix timestamp epoch to DateTime in Ruby Programming| Ruby on Rails by Example