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.

Popularne posty z tego bloga

Ruby vs Kotlin: Custom Comparable classes

re-enable tray icons in ubuntu 13.10?