From: Charles O Nutter Date: 2006-07-30T20:39:08+09:00 Subject: Re: Inconsistency in scoping during module_eval? ------=_Part_10608_18074662.1154259544964 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Why does this: class Foo BAZ = 1 end produce a different result than this: class Foo end Foo.module_eval { BAZ = 1 } On 7/30/06, nobu@ruby-lang.org wrote: > > Hi, > > At Sun, 30 Jul 2006 15:31:27 +0900, > Charles O Nutter wrote in [ruby-core:08441]: > > Why is it that bar is correctly defined under the class Foo, but BAZ > gets > > defined at the top level? Is there a reason for this inconsistency? > > Your code is equivalent to: > > > class Foo > > end > > > > BAZ = 1 > > > > p defined?(bar) => nil > > p defined?(BAZ) => "constant" > > p defined?(Foo::BAZ) => "constant" > > And if you don't use defined?, you'll see a warning like as: > > warning: toplevel constant BAZ referenced by Foo::BAZ > > -- > Nobu Nakada > > -- Contribute to RubySpec! @ www.headius.com/rubyspec Charles Oliver Nutter @ headius.blogspot.com Ruby User @ ruby.mn JRuby Developer @ www.jruby.org Application Architect @ www.ventera.com ------=_Part_10608_18074662.1154259544964--