On Wed, Jul 11, 2012 at 03:43:29AM +0900, John Lee wrote:
> Dear Ruby programmers,
> 
> Well explained. But what good will it do to make a class method? Compare 
> to a instance method.

There are generally two reasons to have a class method, I think (though I
may be missing something).

### Technical Requirements

Sometimes, a method simply has to be a class method rather than an
instance method.  For instance, let's say you have a class that is meant
to represent characters in a computer RPG, and instances of it are
created to represent *specific* characters.  Thus:

    bob_the_blacksmith = CharacterRecord.new(hash_of_stats)
    player_character = Player

The new method obviously won't work as an instance method.

### Conceptual Model

It makes more sense to make a given method a class method than to make it
an instance method.  In the same CRPG, the following situation applies:

    bob_the_blacksmith.likable? CharacterRecord.average_preferences

In the latter case, you're checking the preferences if perfectly average,
default people in your CRPG indicate that bob_the_blacksmith is a likable
guy by the standards of those preferences.  Individual characters might
have different preferences, so a preferences instance method might make
more sense in many circumstances, but when you're just checking averages
it does not really make sense to do so with an instance method because
those average preferences are not in any meaningful way related to
individual characters.

-- 
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]