Project

General

Profile

Actions

Bug #11160

closed

Subclasses of Proc converted when passed to a block

Added by alextyu (Alex Yu) almost 9 years ago. Updated over 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:<unknown>]

Description

As Subclasses of Proc are passed as-is when sent to methods as blocks, it would be expected that the same behavior occurs when passing such a subclass of Proc to a block. However, Ruby's block.call somehow converts the block to a Proc, losing the passed subclass. Is this a bug, or is there a particular reason for this design?

Here is an example:

class MyClass < Proc
end
=> nil

def foo (&x)
p x.class
end
=> :foo

x = Proc.new {|&x| p x.class}
=> #Proc:0x007faf0b042f60@(irb):77

z = MyClass.new {}
=> #MyClass:0x007faf0b0393c0@(irb):78

foo &z
MyClass
=> MyClass

x.call &z
Proc
=> Proc

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

  • Status changed from Open to Closed

This was fixed between Ruby 2.2 and 2.3:

t.rb:

class MyClass < Proc
end
def foo (&x)
  p [:foo, x.class]
end
x = Proc.new {|&x| p [:x, x.class]}
z = MyClass.new {}
foo &z
x.call &z

output:

$ ruby22 t.rb
[:foo, MyClass]
[:x, Proc]

$ ruby23 t.rb
[:foo, MyClass]
[:x, MyClass]
Actions

Also available in: Atom PDF

Like0
Like0