From: dblack@... Date: 2006-04-19T10:50:56+09:00 Subject: Re: Nested scopes Hi -- On Wed, 19 Apr 2006, Yoann Guillot wrote: > Hi, > > I just discovered a behavior of ruby that seems strange to me. > I have a module containing classes, and i'd like those classes to be > able to call some method of the module, so i tried different solutions : > > module Foo > def self.bar > puts "bar!" > end > > bar # works > > class Baz > # bar # doesn't work > # self.bar # doesn't work > Foo.bar # need to know the module name > end > end > > I'd like to be able to call the method from Foo::Baz without knowing the > name of the module, but the only way i found is to call Foo.bar from > inside Baz. > > Now if you replace "module Foo end" by "Module.new.module_eval {}"... > > Shouldn't ruby walk the nesting stack when it doesn't find a method name ? No; it should walk the method lookup path. Inside Baz, self is Baz, and there's no bar method defined in Baz's class (which is Class), nor in Baz's singleton class, nor in any module mixed into either of those. So Baz does not respond to "bar". > Is this going to change in 2.0 ? I don't think it would make sense. Method-calling is determined by the method lookup path of the receiver. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" PDF now on sale! http://www.manning.com/black Paper version coming in early May!