Re: Dynamic Productivity with Ruby
Posted: Dec 10, 2003 3:33 AM
I think this example may be misleading. The point is that, in many languages, you don't need a subclass to change a button's callback method.
I think you should try to extract the concept from this sentence.
When, in most languages, you have to subclass, in ruby you can just change an instance to behave that way. For example, if you have something like
that uses a method like aPerson.current_salary()
you don't need to create a subclass of Person like ChiefExecutiveOfficerPerson to change current_salary() you may just add a singleton method to Person.
Or you can just add a method to an object that is not even
aware of that, like a to_xml() or persistence-related stuf like save()/update()/retrieve()
BTW, I rarely used something like this for common Objects.
The point is that in ruby everything is an Object, and thus you can change the behaviour (or add a new one ) of a Class or of a whole Class hierarchy.
For example, the YAML.rb module, adds methods to the whole ruby world to dump objects as YAML and to retrieve them,
And, class methods are actually singleton methods of Class objects.
It is quite impressive how ruby is based on a few concepts, applied smartly, and singletons are one of those