Bug #13142
closedForwardable regression: cannot delegate to a constant since 2.4.0
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
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
- File forwardable-constant-doc.patch forwardable-constant-doc.patch added
- Status changed from Open to Assigned
- Assignee set to keiju (Keiju Ishitsuka)
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.
Updated by jeremyevans (Jeremy Evans) over 5 years 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]