Brian Mitchell
12/29/2005 1:28:00 AM
On 12/28/05, Payton Swick <payton@foolord.com> 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.
The string is simple though... but if you want another way you could
look at Module#ancestors on the singleton class (since you use
extend).
module A end
a = "some_object".extend A #=> "abc"
singleton_class = class << a; self end # #<Class:#<String:...>>
singleton_class.ancestors #=> [A, String, Enumerable, Comparable,
Object, Kernel]
singleton_class.ancestors.first #=> A
# If you want it in string form:
singleton_class.ancestors.first.to_s #=> "A"
You would want to wrap this up in some sort of method (at least the
class << part).
Brian.