From: jweirich@... Date: 2000-11-14T11:30:42+09:00 Subject: [ruby-talk:6336] Another Smalltalk control structure idea Just as the if_none / when_missing / nil_gives discussion has died down ... Here's another control structure idea from Smalltalk that I didn't see in Ruby. module Enumerable def inject (initial_value) result = initial_value each { |item| result = yield (item, result) } result end end # Example Usage [1,2,3].inject(0) {|item,sum| item+sum} #=> 6 [2,3,4,5].inject(1) {|item,prod| item*prod} #=> 120 [%w(a b), %w(b c)].inject([]) {|item,result| item | result} #=> ["b", "c", "a"] Smalltalk's version is called inject:into:. I'm not particularly happy about the name (its probably too Smalltalk'ish for Matz :-), but I have found the functionality useful. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)