Syndication IconNew article alerts are available via Atom. Hide this message

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

Commenting is closed for this article.

About the boy

A picture of Craig in grayscale

Hi, I'm Craig and I'm a Ruby coder. I live, work and play in London. I like scaling applications and eating yoghurt. Sometimes I climb rocks. Most of the time I climb back down.

You can contact me by email, MSN or Jabber. My address on all of these is craig@xeriom.net.

Code Licence

You can use any of the code on this blog in any way you want. It's totally public domain. You don't even need to attribute it to me, although it would be nice if you did. Just don't sue me.

Friends and colleagues

Other Reading

I Work With Rails

Recommend Me

My Travels

I go places. Do you go places too? Let's meet up!.