Class, Instance and Singleton methods
There are three types of methods in Ruby and I always get them confused. Here, for my future reference, is what I currently think they mean.
Class methods
Methods which can be called directly on a class.
Time.now
Monkey.find(:all)
Instance methods
Methods which can be called on any instance of that class.
@widget.to_s
Time.now.to_f
Singleton methods
Methods which can be called only on a specific instance of that class.
chicken = Chicken.new
class << chicken
def hide
# ...
end
end
chicken.hide
You can verify that I've written this post by following the verification instructions:
curl -LO http://barkingiguana.com/2007/12/20/class-instance-and-singleton-methods.html.orig
curl -LO http://barkingiguana.com/2007/12/20/class-instance-and-singleton-methods.html.orig.asc
gpg --verify class-instance-and-singleton-methods.html.orig{.asc,}
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.