From: Jim Weirich Date: 2006-05-22T23:02:06+09:00 Subject: Re: rubynuby - confused by method name "inject" Regg Mr wrote: Two points: > Seems like "sum" would have been a better name. Unless the operator isn't + ... [1,2,3].inject { |left, right| left * right } # => 1*2*3 "A::B".split("::").inject(Object) { |left, right| left.const_get(right) } # => Object.const_get("A").const_get("B") (2..n).inject(1) { |left, right| left * right } # => n! => 2*3* ... * n > Also > > [1,2,3].each {|a| b += a} > > is easier to read ...for me. Perhaps, but there is a fundamental difference between the inject and each versions. The "each" verison is procedural. It defines state (the value of b) and how that state changes in each iteration (b += a). The "inject" version is functional. There are no extra state variables in our code that need initialization. And the expression itself is the value, rather than having a side effect on a local variable. Not that one way is better than the other, just noting differences. -- Jim Weirich -- Posted via http://www.ruby-forum.com/.