From: dblack@... Date: 2005-12-29T11:03:54+09:00 Subject: Re: get mixin's name Hi -- On Thu, 29 Dec 2005, 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" Does this help? module Awesome s = self define_method("who_am_i") { s } end obj = Object.new obj.extend(Awesome) p obj.who_am_i # Awesome There's a bit of semantic weirdness in the object claiming to be the module :-) But anyway, that's one way to get the module object into one of its own instance methods. David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black