Bug #5761
closedArray#flatten(N) calls to_ary on the (N+1)-level objects
Description
$ cat flatten.rb
class Foo
def respond_to?(name, include_private = true)
puts "respond_to?(#{name})"
super
end
def method_missing(name, *args)
puts "method_missing(#{name})"
super
end
end
puts "[[Foo.new]].flatten(1)"
p [[Foo.new]].flatten(1)
puts
puts "[[[Foo.new]]].flatten(2)"
p [[[Foo.new]]].flatten(2)
puts
puts "[[[Foo.new]]].flatten(1)"
p [[[Foo.new]]].flatten(1)
$ ruby flatten.rb
[[Foo.new]].flatten(1)
method_missing(to_ary)
respond_to?(to_ary)
[#Foo:0x00000001ae41c8]
[[[Foo.new]]].flatten(2)
method_missing(to_ary)
respond_to?(to_ary)
[#Foo:0x00000001ae3d90]
[[[Foo.new]]].flatten(1)
#<a href="Foo:0x00000001ae3980" class="external">Foo:0x00000001ae3980</a>
$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Expected behaviour: no method calls are made on the (N+1)-level objects.
For what it's worth, this is the 1.8.7 output:
$ ruby flatten.rb
[[Foo.new]].flatten(1)
respond_to?(to_ary)
[#Foo:0x7fef3ebb1888]
[[[Foo.new]]].flatten(2)
respond_to?(to_ary)
[#Foo:0x7fef3ebb16a8]
[[[Foo.new]]].flatten(1)
#<a href="Foo:0x7fef3ebb14f0" class="external">Foo:0x7fef3ebb14f0</a>