Bug #13623
closedmeta-programming, adding sub-module fails with syntax error
Description
The goal is to add a sub-module to a higher level module. The higher level module is derived from its name in a string. This works in one case and gives an unexpected syntax error in another case.
failing_meta:4: syntax error, unexpected '\n', expecting &. or :: or '[' or '.'
failing_meta:8: syntax error, unexpected keyword_end, expecting end-of-input
Files
Updated by caschip (Aad Schippers) almost 7 years ago
module Foo
end
# Failing with syntax error is the code below
module Object::const_get( 'Foo' )
module Bar
end
end
Updated by shyouhei (Shyouhei Urabe) almost 7 years ago
Well const_get('Foo')
is syntactically a non-constant while const_get('Foo')::Bar
is. This is the ultimate reason why working_meta works but failing_meta fails. If you want to meta-program you have to be explicit like const_get('Foo').module_eval { ... }
.
Updated by caschip (Aad Schippers) almost 7 years ago
So you are saying this is how it should work and not a bug? If that is true, I should close this. What do you mean when you say that Object::const_get('Foo')::Bar
is syntactically a constant. Its contains characters that are not appropriate in constant names, like brackets and single quotes, and a method is called, const_get
, to evaluate it. To me it is an expression a::b::c
with two scope operators, two constants (Object
and Bar
) and one method call that returns a constant const_get('Foo')
=> Foo
.
Updated by nobu (Nobuyoshi Nakada) almost 7 years ago
- Description updated (diff)
- Status changed from Open to Rejected
module
statement requires a name, not a constant object.
Updated by caschip (Aad Schippers) almost 7 years ago
Ok, you make it very clear. Indeed, in working_meta 'Bar
' is not a constant but a name when statement 'module
' is evaluated. Statement 'module
' syntactically requires a name that does not exist in the scope yet. That name can be preceded by an expression that results in a scope. Default is the current scope. Name and scope expression are separated by double colon.
Updated by nobu (Nobuyoshi Nakada) almost 7 years ago
caschip (Aad Schippers) wrote:
Statement '
module
' requires a name that does not exist in the scope yet.
A (small) correction: it may exist syntactically,
$ ruby -c -e 'X=1; module X; end'
Syntax OK
but a TypeError
will be raised at runtime if it is not a Module
.
$ ruby -e 'X=1; module X; end'
Traceback (most recent call last):
-e:1:in `<main>': X is not a module (TypeError)