From: Mathieu Bouchard Date: 2005-01-27T01:06:42+09:00 Subject: Re: Self and Ruby Comparisons On Tue, 25 Jan 2005, Robert Klemme wrote: > > Actually, Perl has it better, because you can change the class of an > > object, and you can change the list of superclasses of any class. > And if a copy is sufficient you can do this even in pure Ruby: No, a copy is _not_ sufficient. > Do you have a link or explanation that demonstrates the merits of the > capability to change an instance's class at runtime? Currently I don't > have a clue in which situations I would want this. Thx! Just like almost any other programming concept invented to improve structured programming, it's about replacing if/case expressions by tables functionpointers such that it creates a "separation of concerns". (i mean all the various forms of polymorphisms) Now, for that feature in particular, think of a File which may be open or closed, and you have to write at the start of each damn method: if not @file then raise "file not open" end well, this could be solved by changing the set of all methods of that class. In general an object that has a few major cases of behaviour like that, can benefit from switching methodtables. In Ruby, my way to solve this problem is instead to use one object representing a File that may be open or not, and one object that represents a File guaranteed to be open, and then use message-forwarding using Object#method_missing. _____________________________________________________________________ Mathieu Bouchard -=- Montr�al QC Canada -=- http://artengine.ca/matju