Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "puts" return a blank line, but not "puts []"?

Tags:

arrays

io

ruby

puts

I'm going through a Ruby tutorial, and learned that the code

puts 'start'
puts
puts 'end'

will output three lines, but the following code

puts 'start'
puts []
puts 'end'

will only output two. The stated reason is that [] isn't an object (edit: "doesn't point to anything"), so puts can't do anything with it, but why is that not also true in the first case?

I tried to find an official page about puts to figure this out, and this one was no help.

like image 504
Zach Avatar asked Dec 17 '25 07:12

Zach


1 Answers

The stated reason is that [] isn't an object

Stated where?

puts has a special handling for arrays. When you pass it an array, it prints each element on a new line. You pass it an array with zero elements, it prints zero lines.

like image 173
Sergio Tulentsev Avatar answered Dec 19 '25 21:12

Sergio Tulentsev