tags:

views:

73

answers:

2

Fairly new to ruby, can someone explain why these 2 things respond differently?

a=["A","B","C"]
puts a
A
B
C

puts "#{a}"
ABC

a.to_s returns the same output as the templating output, but shouldn't the simple "puts a" do the same?

+4  A: 

The specified behavior of puts is that it writes stuff out with a newline afterwards. If it's an array, it writes each element with a newline.

When you do puts a.to_s, it does the to_s first (resulting in a single string) and then outputs that single string with a newline afterward.

JacobM
+3  A: 

As discussed in this thread, and for no good reason, Arrays have magically inconsistent behavior when given to puts.

array.each {|e| puts e }

is the same as:

puts array

Jonathan Feinberg