From: Gavin Sinclair Date: 2003-09-03T10:41:05+09:00 Subject: Re: OOP flavours - was Python vs. Ruby > On Wed, 3 Sep 2003 09:33:02 +0900 > David Naseby wrote: > > >> I like this style too, and Ruby allows one to do it well via Mixin >> Oriented Programming (MOP?). The best library based example I've seen >> is in Amrita, where you extend your model class with >> Amrita::ExpandByMember, so amrita can render the object correctly. >> >> Similarly, you don't need to expand classes to predict there uses - >> for the example of IO printing flying around, you can extend on the >> fly: >> module STDOUTPrinter >> def print >> STDOUT.print self.to_str >> end >> end >> >> str = "helloworld" >> str.extend STDOUTPrinter >> str.print >> >> Its a fairly useless example in this case, but extend this to template >> based GUIs along the amrita lines, and mixin oriented programming >> shows its real strength. > > > Actually this isn't a very good example. This whole argument about > "print should be a member of an object" is demonstrably inelegant and > wrong, for a number of reasons (some of which have been noted already): > > [...] I'm surprised nobody has mentioned the built-in method Object#display. It is analogous to String#print, and gives the lie to the notion that you need to also implement Fixnum#print, Range#print, etc. --------------------------------------------------------- Object#display obj.display( port=$> ) -> nil ------------------------------------------------------------------------ Prints obj on the given port (default $>). Equivalent to: def display(port=$>) port.write self end For example: 1.display "cat".display [ 4, 5, 6 ].display puts produces: 1cat456 Personally I never use it, for all the reasons that have been given, and one more: I prefer #puts to #write. Gavin