Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Scope problem (?) in implementing Design Patterns in Ruby Date: Wed, 11 May 2011 15:23:41 -0500 Organization: Service de news de lacave.net Lines: 86 Message-ID: <4bfa1e5a3794daf70f5171094052dab0@ruby-forum.com> References: <7e6155b0-8d33-4677-87ec-be2c3f0fca97@b7g2000prg.googlegroups.com> <2ac328808edcaa956da4f286617acefb@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1305145456 45213 65.111.164.187 (11 May 2011 20:24:16 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Wed, 11 May 2011 20:24:16 +0000 (UTC) In-Reply-To: <2ac328808edcaa956da4f286617acefb@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 383004 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <4bfa1e5a3794daf70f5171094052dab0@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:4292 7stud -- wrote in post #998081: > RichardOnRails wrote in post #998059: >> > > 1) Your case statement syntax doesn't work in ruby 1.9.2: > > prog.rb:4: syntax error, unexpected ':', expecting keyword_then or ',' > or ';' or '\n' > > You have to use 'then' in place of a colon. > > 2) Next, I get this error: > > prog.rb:67:in `evaluate': uninitialized constant Not::All (NameError) > from prog.rb:74:in `
' > > which relates to this code: > > class Not < Expression > def initialize(expression) > @expression = expression > end > > def evaluate(dir) > all = All.new.evaluate(dir) > other = @expression.evaluate(dir) > all - other > end > end > > In ruby, constants are looked up like directories and files (or if you > prefer constants are 'lexically scoped'). When you > are inside the Not class (which is a module), the 'directory' you are in > for constant lookups is the 'Not' directory. When you write All.new, > because the name All is not preceded by a directory name, ruby looks in > the current 'directory' for the constant All. The current directory is > Not, so ruby is looking for Not::All, i.e. the 'file' All in the > 'directory' Not. However, All is not defined inside Not, so you get an > error. In fact, there is no constant named All defined anywhere in > your program, so the error is more serious than a scope problem. > > If you are inside a class/module and you need to access a class at the > top level, you do this: > > class All > def greet > puts 'hi' > end > end > > > class Dog > def do_stuff > ::All.new.greet #<***** > end > end > > Dog.new.do_stuff #=>hi > In fact, that is unnecessary: class All def greet puts 'hi' end end class Dog def do_stuff All.new.greet #<***** end end Dog.new.do_stuff #=>hi I guess the lookup actually starts at the toplevel. So your error is a result of not defining the constant All at the toplevel, and ruby obfuscates the error by telling you that All is not defined inside the Not module, giving you the error message: Not::All doesn't exist. -- Posted via http://www.ruby-forum.com/.