Array.to_s doesn't recurse on items in 2.1.1?

Hi,

I found some strange behaviour in Ruby 2.1.1 (compared to 1.9.3) on
Linux: Array.to_s doesn’t recursively call to_s on each item.

Below is a sample script which demonstrates the behaviour, and shows the
differences between 1.9.3 and 2.1.1.

What was the reasoning behind this change? Is it a bug?

Regards,

–Ashiq

Fails on ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]

Array.to_s doesn’t call Object.to_s

class Cat
attr_reader :name
def initialize(name)
@name = name
end

def to_s
return “#{@name} the cute kitty”
end
end

These two should be the same (sans square brackets)

Works in 1.9.3, not in 2.1.1

c = Cat.new(“Mittens”)
puts “Object: #{c}” # Mittens the cute kitty
puts “Array: #{[c]}” # [<Cat:0x00000001841d08 @name=“Mittens”>]

c = Cat.new(“Mittens”)

Here you call c.to_s

puts “Object: #{c}” # Mittens the cute kitty

Here you call [c.inspect].to_s

puts “Array: #{[c]}” # [<Cat:0x00000001841d08 @name=“Mittens”>]

It gives the expected output, I consider the previous version as you
described it as incorrect and unexpected.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs