How to access a class method from an instance

by Frank

If you bet that this post would fall in the “short & sweet” category, you won… a new car! (The price is right music starts while Bob Barker looks at you with a subtile smile)

To access a class method from an instance, you have to use a reference to that class (because that’s where the method resides). The attribute reader returning that reference is named class :

class Monkey
  def Monkey.laughs
     "Ou ou ou Ahh ahh AHHH!" #that's how a monkey laughs
  end
end

mvp = Monkey.new
mvp.class.laughs
Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon