Issue #12319 has been updated by Tsuyoshi Sawada.


Martin Drst wrote:
> Strings can have lots of structure; Symbols essentially don't have structure.

If you look at the literal `:"B::C"`, then it might seem somewhat contrived, but in practical use, the argument passed to `const_get` can be (and in most case is) a variable. And since it is more common to express method or constant names as symbols rather than strings, it is natural that the relevant variable argument turns out to be a symbol (perhaps derived somewhere in the code from a string via `to_sym`). If it did not accept symbol at all, then it is easy to be reminded to just apply `to_s` to it, but if symbol works sometimes, then `to_s` can be easily forgotten. Then, later during the program run, when the variable turns out to hold a symbol representing a nested name, it suddenly raises a problem.

----------------------------------------
Feature #12319: `Module#const_get` does not accept symbol with nested name
https://bugs.ruby-lang.org/issues/12319#change-58329

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
`Module#const_get` accepts non-nested string, nested string, and non-nested symbol:

~~~ruby
class A; class B; class C end end end

A.const_get("B")    # => A::B
A.const_get("B::C") # => A::B::C
A.const_get(:B)     # => A::B
~~~

but does not accept nested symbol:

~~~ruby
A.const_get(:"B::C") # => NameError: wrong constant name B::C
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>