Project

General

Profile

Actions

Bug #13623

closed

meta-programming, adding sub-module fails with syntax error

Added by caschip (Aad Schippers) almost 7 years ago. Updated almost 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
[ruby-core:81528]

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

no_meta (72 Bytes) no_meta Gets to the goal without meta-programming caschip (Aad Schippers), 06/02/2017 02:35 PM
working_meta (82 Bytes) working_meta Gets to the goal with meta-programming caschip (Aad Schippers), 06/02/2017 02:35 PM
failing_meta (95 Bytes) failing_meta Fails to get to the goal. Gives a syntax error caschip (Aad Schippers), 06/02/2017 02:35 PM

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)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0