From: Simen Edvardsen Date: 2007-03-17T07:34:06+09:00 Subject: Re: Anyone playing with higher order messaging in ruby? On 3/14/07, Christoffer Lern� wrote: > For example, something that I often want to do is: > > array.each { |entry| entry.do_something(1, "a") } > > [...] > > Other people must have played around with this. > > I'd like to learn more about using these methods, so are there any > links and sites people could share with me? > > > /Christoffer > I wrote this some time ago: class Message def initialize(message, *args) @message, @args = message, args end def call(x, *args) x.send @message, *(@args + args) end def to_proc lambda { |*args| call(*args) } end end Now you can do: puts [*0..100].select(&Message.new(:>, 50)).inspect puts [*0..100].inject(&Message.new(:+)) puts %w(Once upon a time).map(&Message.new(:upcase)).inspect puts Message.new(:index, /[aeiou]/, -3).call("hello") puts %w(1 2 3 4 5).map(&Message.new(:to_i)).map(&Message.new(:class)).inspect The &Message.new syntax is a bit verbose, so you could alias it: def _(msg, *args) Message.new(msg, *args) end And then it runs smooth: puts [*0..100].select(&_(:>, 50)).inspect I don't know if this helps. It does do away with the need for method_missing or changing core classes or stuff like that. I don't know how useful it is, because I've never used it for anything serious. If it was to be really useful, it should've been incorporated into the language and standard libraries. -- - Simen