From: Jason Roelofs Date: 2007-03-15T04:57:03+09:00 Subject: Re: Anyone playing with higher order messaging in ruby? ------=_Part_53956_10799267.1173901817020 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I haven't personally messed with this kind of stuff but code like this just makes me love Ruby even more. These types of, well DSL is really the best way to describe it, in such little code, makes Ruby such a blast to program in, and it's readable too! Jason On 3/14/07, Christoffer Lern=F6 wrote: > > On Mar 14, 2007, at 20:04 , Christoffer Lern=F6 wrote: > > > In a more generalized case, you want to use a block to act as a class. > > > > module Trampolines > > class Trampoline > > def initialize(block) > > @block =3D block > > end > > def method_missing(symbol, *args, &block) > > args << block if block > > @block.call(symbol, *args) > > 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 > > For example > > ["a", "b", "c"].reject_do[/a/] # =3D> ["b", "c"] > ["a", "b", "c"].collect_do.capitalize #=3D> ["A", "B", "C"] > > > > ------=_Part_53956_10799267.1173901817020--