passing lambda when expecting an enumerable

Here are two methods defined in the Proc class, designed to be used
for functional programming:

  def apply(enum) &self
  alias | apply

  def reduce(enum)
    enum.inject &self
  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?

ok I see what it;s doing. Its first calculating this part:


to check how far the elements deviate from the mean and returns an
array of the differences.

Then what happens next is that the square| is invoked on the returned
array from above:


So it squares the result set, which returns an array of the
deviation_returned_array squared. THen we invoke reduce passing
squared_deviation_returned_array enumerable to the sum proc, which
just totals the result set:


And then divide by (a.size-1) and take the square root of it.

So I guess the lesson here is not only do parentheses indicate
priority in evaluating an expression, but the expressions next to the
ones in parentheses are evaluated before starting to evaluate from
left to right.