class Proc
def compose(&blk)
self.class.new { |*argv| blk.call(self.call(*argv)) }
end
alias :comp :compose
end
one_to_one = proc { |name| name + " trick" }.comp(&:upcase)
two_to_one = proc { |desc| desc.upcase }.compose { |name, age| "name: #{name} age: #{age}" }
puts one_to_one.call("dumb")
puts two_to_one.call("person", 37)
Os resultados acima:
$ DUMB TRICK
$ NAME: PERSON AGE: 37