From: Sean O'Halpin Date: 2006-07-02T09:52:08+09:00 Subject: Re: Arrray#to_proc On 7/1/06, Alex Young wrote: > Matthew Harris wrote: > > I don't know if this was posted previously, but I had the idea of a > > Array#to_proc, an example would be: > > > > [9, 19, 29].map(&[:succ, :to_s, :reverse]) > > => ["01", "02", "03"] > > > > I've seen people ask questions such as arr.map(&'meth1.meth2.meth3') > > but I think using the Array#to_proc solution is cleaner. > > > > Would this actually prove useful? It would seem that using the normal > > block form would be just as much typing, though. > > > I'd rather do this: > > class Proc > def +(other) > proc {|*args| > args = [self.call(*args)] > other.call(*args) > } > end > end > > [9, 19, 29].map( &:succ + &:to_s + &:reverse ) > #=> ["01", "02", "03"] > > but I'm just weird like that :-) > > Is this useful for anything? It looks like it should be, but I can't > think of what... > > -- > Alex Hi, I get a "parse error, unexpected tAMPER" with ruby 1.8.4 (2005-12-24) [i386-mswin32]. What version are you using? How about something like this: module Enumerable def cascade(*methods) methods.inject(self) { |ary, method| ary.map{ |x| x.send(method)}} end end res = [9, 19, 29].cascade :succ, :to_s, :reverse p res #=> ["01", "02", "03"] Unless you prefer the line noise of course ;) Regards, Sean