0

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.