From: Ned Konz Date: 2001-08-08T14:05:20+09:00 Subject: [ruby-talk:19346] Re: Perl/Python/Ruby common backend (Parrot, can Ruby play too?) On Tuesday 07 August 2001 09:35 pm, Andrew wrote: > I do want to emphasize that this isn't a need of mine. I brought > this up as part of a Ruby/Python comparison, and I was stating that > I don't think adding new methods to an existing class is a good > practice. Python's "magic incantation" as you call it is more > the way I prefer things, unlike in Ruby where this sort of technique > seems to be encouraged. Adding methods to existing classes is a very useful idiom. It's used in Smalltalk all the time, and is even supported explicitly by one of the leading big-team configuration management systems (Envy/Developer). The idea is that one can extend classes to add interfaces to the rest of a package. So the package both defines new classes and extends existing ones. Typically this is extension, rather than modification of existing methods (in fact, Envy will complain if you try to modify an existing method.) This eases separation of concerns (also what Aspect-oriented programming is about). I just had a conversation on the Squeak list about something just like this. Imagine you have an existing graphics library. Users could have developed their own shapes. Now you want to add a set of tools of some kind, and the graphics library doesn't know about tools. You can do this by merely adding the tool-interaction methods to the base class of the graphics objects, and adding the tool classes themselves. This way I don't have to go back to the original definition of the class, and systems will run fine with or without the extension. And the tools work with every graphic object in the system, whether or not it knows about the tools. You can never know how someone's going to extend a system. So you can never know all the interfaces a particular class is going to need to have. And since you may not have access to the original class (or may not want to change it), extending it by adding methods is a great way to go. -- Ned Konz currently: Stanwood, WA email: ned@bike-nomad.com homepage: http://bike-nomad.com