From: Payton Swick Date: 2005-12-30T01:25:14+09:00 Subject: Re: get mixin's name The goal was basically this for a GUI (the answer is probably that there's a better way): module Layout # ... layout stuff def layout_name 'none' end end module CoolLayout include Layout # ... override some layout stuff. def layout_name 'CoolLayout' # Not DRY end end class Window def layout=(l) extend l end end a = Window.new a.layout = CoolLayout puts "This window has the layout #{a.layout_name}" -Payton Robert Klemme wrote: > 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 >