Search code examples
rubyarraysioputs

Why does "puts" return a blank line, but not "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.


Solution

  • 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.