Ruby on Rails Monday, December 1, 2014



On Monday, December 1, 2014 2:07:58 PM UTC, trekr67 wrote:
[snip]
 
::NAME = 'gginfrausermanager'
[snip] 
and want to call this variable in another file2.rb with the following code:

#file2.rb

require './file1.rb'
include GG

p "#{GG::NAME} is called from file1.rb"

However when I do a ruby file2.rb I get uninitialized constant GG::NAME.

Can you tell me what I'm doing wrong?



There are two things here. First, because you've done ::NAME = (instead of NAME=) this has set the constant at the top level: you've created Object::NAME, not GG::GG::NAME

The second thing is that constant lookup always looks at the current scoping before it starts walking up the lexical scope chain or the ancestry chain: even after you include GG, GG::NAME is referring to the top level GG (i.e. the module not the class)

Fred 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/065ae6a4-454d-4477-b307-127b13120934%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment