From: Robert Klemme Date: 2005-12-29T19:57:54+09:00 Subject: Re: get mixin's name Payton Swick wrote: > A somewhat weird situation. > > I have a module which I include in a class using Module#extend. That > module defines an instance method which returns the name of the module > that defined the method. Currently, I do that by hard-coding the > string into the method definition, but it seems that there should > really be a better way. > > Here's some code: > > irb(main):001:0> module Awesome > irb(main):002:1> def who_am_i > irb(main):003:2> 'Awesome' # Repeating myself, bad! > irb(main):004:2> end > irb(main):005:1> end > => nil > irb(main):006:0> class One > irb(main):007:1> def initialize(what) > irb(main):008:2> extend what > irb(main):009:2> end > irb(main):010:1> end > => nil > irb(main):011:0> a = One.new(Awesome) > => # > irb(main):012:0> a.who_am_i > => "Awesome" > > -Payton If you want to find out the defining class of a method you can use #method: >> module Awesome; def foo() end end => nil >> class One; include Awesome; end => One >> One.new.method(:foo).to_s => "#" Btw, what do you need that for? Kind regards robert