From: "Mauricio Fernández" Date: 2005-09-15T19:38:36+09:00 Subject: Re: defining a method relative to a module's nesting On Thu, Sep 15, 2005 at 01:36:34PM +0900, Trans wrote: > module X > def self.included(mod) > define_method ... # define method from here > end > end > > module N > # <- define method to this namespace (N) > module M > include X > end > end batsman@tux-chan:~/src/ruby/ruby.head$ cat /tmp/fgdsdgdfg.rb module X def self.included(mod) # for top-level module, parent = self; change for other semantics parent = Object.const_get(mod.to_s.sub(/::[^:]+\z/, "")) parent.send(:define_method, :foo){ puts "#{parent.to_s}#foo" } #fcall in 1.9 class << parent; self end.send(:define_method, :bar) do #fcall in 1.9 puts "#{parent.to_s}.bar" end end end module M module N include X end end Class.new{ include M }.new.foo M.bar batsman@tux-chan:~/src/ruby/ruby.head$ ruby /tmp/fgdsdgdfg.rb M#foo M.bar -- Mauricio Fernandez