Project

General

Profile

Bug #13142

Forwardable regression: cannot delegate to a constant since 2.4.0

Added by ch1c0t (Anatoly Chernow) over 2 years ago. Updated 23 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:79207]

Description

Before this commit

https://github.com/ch1c0t/tra/commit/96b15f042183f488413db99d6ede304d20503cbd

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
               Enumerable
        from test.rb:16:in `<main>'

I report it because it might be an unintentional regression.


Files

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

Associated revisions

Revision 4b7d7d00
Added by jeremyevans (Jeremy Evans) 23 days ago

Document and add spec for delegating to constants in Forwardable

Fixes [Bug #13142]

History

Updated by jeremyevans0 (Jeremy Evans) about 2 months 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 M.new.to_s'
"1"

$ ruby22 -rforwardable -e 'class M; ENUMERATOR = 1; extend Forwardable; delegate [:to_s] => :ENUMERATOR; end; p M.new.to_s'
-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 M.new.to_s'
"1"

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

#2

Updated by jeremyevans (Jeremy Evans) 23 days 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