Here are two methods defined in the Proc class, designed to be used for functional programming:
def apply(enum) enum.map &self end alias | apply
def reduce(enum) enum.inject &self end alias <= reduce
Here's an application of them:
sum = lambda {|x,y| x+y } mean = (sum<=a)/a.size deviation = lambda {|x| x-mean } square = lambda {|x| x*x } standardDeviation = Math.sqrt((sum<=square|(deviation|a))/(a.size-1))
This excerpt is taken from "The Ruby Programming Language".
Here's the thing. In the last line of the above code it uses "sum<=square". That reduce method expects an enumerable as an argument. And instead it is being sent a lambda held in square variable. This should fail because the inject method does not exist on the lambda. It exists on arrays, and other enumerable types. So what am I missing here?