From: Joseph McDonald Date: 2001-08-15T01:32:02+09:00 Subject: [ruby-talk:19706] Re: class methods and optimizing CM> Does anyone know what class methods are as opposed to instance methods? Are CM> they for optimizing? when would I use them?I have looked at a couple manuals CM> that tell you how to do them: def classname.method but none seem to explain CM> the why and when. I asked a similar question in the past and Hal Fulton sent me this highly excellent explanation: -------------- When a method describes functionality that should belong to an individual object (an instance), it should be an instance method. When the functionality is NOT tied to a specific instance, it should be a class method. For a built-in example: Why is chmod a class method of File? Well, it's because doing a chmod on a file really needs no other information besides the name. Why should you have to create a file object just to do this operation? (Of course, there is also an instance method of the same name -- meaning you *can* do it to an instance of File, probably one you created for some other purpose.) File.chmod(0555, "myfile") # No File object in existence! By the way, it's not uncommon for a class method to return an instance of the class. For example, Something.new (in general ) returns a Something object; Thread.fork returns a thread; Regexp.compile returns a regular expression; and so on. It's perhaps easier to understand for data than for methods. Is the scope of this piece of data a single object? Or is it the entire class? For example, there is a Boolean value that controls whether threads abort when an exception occurs. It doesn't apply to just one thread; it's an attribute of the entire set of threads -- an attribute of the class. Thus we can check it or set it with class methods: flag = Thread.abort_on_exception Thread.abort_on_exception = true The classic example of class data is to count the number of objects created. This is possible if we keep a single counter for the class which is incremented for each object. ------------- CM> And this leads to my second question. In terms of memory usage and speed, is CM> it better to extend the existing classes (when possible) or to build your CM> own, or to just make a subclass? e.g. If I program a deck routine for a card CM> game I can just add shuffle, play etc. routines to class Array. Now the CM> computer isn't holding an extra class definition in memory, but ALL arrays CM> (not decks) are also extended. If I make my own class/subclass, not all of CM> the arrays are extended but there is another class definition in memory... I don't know about memory usage or speed, but I would be wary of changing (or adding to) the functionality of standard classes that everyone relies on, because if your code gets used by someone else, it may cause unexpected results. It would be nice if you could somehow limit the scope of your changes to a class. That way, if you wanted to change the way Array works, you can do it without breaking other code used in the same program that changes Array in a different way. I'm not sure if such a thing is possible or even considered a good idea by matz, but I think it's a good idea for now... thanks, -joe