Bug #13142

Forwardable regression: cannot delegate to a constant since 2.4.0

Added by ch1c0t (Anatoly Chernow) almost 4 years ago. Updated about 1 year ago.

Target version:


Before this commit

it works just fine on 2.3. But on 2.4.0:

/usr/lib/ruby/2.4.0/forwardable.rb:222:in `take': uninitialized constant ENUMERATOR (NameError)
Did you mean?  Enumerator
        from test.rb:16:in `<main>'

I report it because it might be an unintentional regression.


forwardable-constant-doc.patch (2.72 KB) forwardable-constant-doc.patch jeremyevans0 (Jeremy Evans), 06/24/2019 10:08 PM

Updated by jeremyevans0 (Jeremy Evans) over 1 year ago

From my testing, it looks like Ruby 2.2 was the first release to stop supporting nested constant lookup in delegate:

$ ruby21 -rforwardable -e 'class M; ENUMERATOR = 1; extend Forwardable; delegate [:to_s] => :ENUMERATOR; end; p'

$ ruby22 -rforwardable -e 'class M; ENUMERATOR = 1; extend Forwardable; delegate [:to_s] => :ENUMERATOR; end; p'
-e:1:in `<main>': uninitialized constant ENUMERATOR (NameError)

The documentation specifies that the argument to delegate should be a symbol or array of symbols, and the symbols correspond to method names (not constant names). However, as indicated by the error message, it turns out the constant names are supported, but you need to provide the full path of the constant:

ruby27 -rforwardable -e 'class M; ENUMERATOR = 1; extend Forwardable; delegate [:to_s] => "M::ENUMERATOR"; end; p'

Attached is a patch to document how constant names are supported, and to add a test for the use of constants in forwardable.


Updated by jeremyevans (Jeremy Evans) about 1 year ago

  • Status changed from Assigned to Closed

Applied in changeset git|4b7d7d007fa5a06d237be6f379106feea25fca79.

Document and add spec for delegating to constants in Forwardable

Fixes [Bug #13142]

Also available in: Atom PDF