views:

67

answers:

2

How I can get from a module the Class name of the class the module is included ?

module ActMethods
  def some_method(*attr_names)
    cls = self.class # this doesn't work 
  end
end

How I can get into the cls variable the name of the class to with this module is loaded ?

+4  A: 

self.class does get you the class of the object the method is called on. Assuming the module was included into a class, this either is the class that included the module or a subclass thereof. If you really just want the name, you can use self.class.name instead.

If you extended a class with the module and you want to get that class, you can just do cls = self (or cls = name if you want the name of the class as a string).

If none of the above helps, you should clarify what you want.

sepp2k
+1  A: 

Works for me. As sepp said you have to include it for it to work.

module ActMethods
  def some_method(*attr_names)
    cls = self.class # this doesn't work 
    puts cls
  end
end

class Boh
  include ActMethods
end

b = Boh.new
b.some_method
Chuck Vose