From: Pixel Date: 2000-02-15T13:37:57+01:00 Subject: [ruby-talk:01378] Re: nice sample for functional stuff gotoken@math.sci.hokudai.ac.jp (GOTO Kentaro) writes: [...] > > def sum > > inject(0) {|n, i| n += i } ^^ why ?? inject(0) {|n, i| n + i } seems enough to me, uh? > > end > > end > > That is not bad though, I was interested in how I can sum up the > non-numerical elements. The solution above uses an assumption such > that the result is 0 if enum.size == 0 and also that each element are numbers, otherwise you need def sum inject(0) {|n, i| n + i.to_i } end but doesn't help much > but [ruby-talk:01364] doesn't. uh? your solution gives ([].sum == nil), tis still not the best. mine has the same problem l = [1] l.sum + 1 #=> 2 l.shift l.sum + 1 #=> Exception -> [].sum should raise an exception (perl has no such problem as "" is 0 is undef is ... :)