There were several good discussions on c.l.r today. First up is this one on Message-Object-Oriented compared to Function-Object-Oriented. I was reading the originating article just the other day. Anyway, the guy, James, that started the thread on c.l.r came up with a way to make Ruby syntax more like MOO. It's ugly, but it shows how easy it is to manipulate Ruby. Here's the magic:
I set about finding a way to do it in Ruby, and came up with an addition to the Symbol class. I wasn't sure if I wanted to have a full-blown Message class; I liked the idea that one could just type the message to send without having to go create a special object for it. The Symbol syntax makes this very natural
class Symbol def >>( *args ) ary = [] arglist = (block_given? ? yield : nil ) args.each{ |obj| begin ary << dispatch( obj, arglist ) rescue Exception ary << $!.clone end } ary end
def dispatch( obj, arglist ) return obj.send(self.to_s, *arglist) if arglist && (arglist.size>0) obj.send( self.to_s ) end end
This allows expressions like:
:some_message.>> obj :some_message.>>( obj1, obj2 ) :some_message.>>( obj1, obj2 ){ [ arg1, arg2, arg3] }
Any message string, instantiated as a Symbol, can be dispatched to a list of objects, along with a set of message arguments. The results come back as an array.
The second thread is about implementing Ruby in Ruby. This is the same idea demonstrated by creating a Lisp interpreter in Lisp. Dan Sugalski (of Parrot fame) started an interesting thread. See this for more along those lines.
9:41:50 PM
|
|