Project

General

Profile

Actions

Bug #5761

closed

Array#flatten(N) calls to_ary on the (N+1)-level objects

Added by jonleighton (Jon Leighton) almost 13 years ago. Updated over 12 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Backport:
[ruby-core:41645]

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>


Related issues 1 (0 open1 closed)

Copied to Ruby master - Bug #10748: Array#flatten(N) calls to_ary on the (N+1)-level objectsClosedmame (Yusuke Endoh)12/14/2011Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0