P
Payton Swick
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)
=> #<One:0x40322710>
irb(main):012:0> a.who_am_i
=> "Awesome"
-Payton
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)
=> #<One:0x40322710>
irb(main):012:0> a.who_am_i
=> "Awesome"
-Payton