From: Jenda Krynicky Date: 2007-03-17T02:32:48+09:00 Subject: Re: Anyone playing with higher order messaging in ruby? Christoffer Lernö wrote: > On Mar 14, 2007, at 20:04 , Christoffer Lern� wrote: > >> end >> end >> end >> >> def trampoline(&block) >> raise "Missing block" unless block >> Trampolines::Trampoline.new(block) >> end > > This code adds methods to all enumerables. > > module Enumerable > %w(each collect select reject).each do |method| > class_eval "def #{method}_do; trampoline { |m, b, *a| #{method} > { |o| o.__send__(m, *a, &b) } }; end" > end > end Looks to me that it's easier to extend the Forwarder a bit and implement this like this: class Forwarder def initialize(obj, method) @obj = obj @method = method end def method_missing(symbol, *args) @obj.__send__( @method) { |entry| entry.__send__(symbol, *args) } end end module Enumerable %w(each collect select reject).each do |method| class_eval "def #{method}_do; Forwarder.new(self, :#{method}) end" end end it's a shame the builtins do not work like this already. And while it's possible to overwrite the "collect", "select" and "reject" in Enumerable, to overwrite the "each" you'd (AFAIK) have to touch all the individual classes. I should not have to name something I only use once: list.each { |x| x.whatever() } Jenda -- Posted via http://www.ruby-forum.com/.