piątek, 29 października 2010

Array#to_proc - a little experiment


class Array
def to_proc
Proc.new{|obj| self.map{ |e| obj.send(e) } }
end
end

["foo", "bar"].map(&[:upcase, :capitalize])
=> [["FOO", "Foo"], ["BAR", "Bar"]]


How do you like it ?

We could also go this way:


class Array
def to_proc
Proc.new{|obj| self.map{ |e| e.to_proc.call(obj) } }
end
end


Maybe not the fastest thing in the world but definitely elegant.