From: Robert Klemme Date: 2005-05-13T16:25:28+09:00 Subject: Re: {} vs begin/end [was Re: object loops and what they return] Martin DeMello wrote: > Brian Schr�der wrote: >> >> print "100! = " >> puts (1..100).inject(1) { |r,v| >> r*v >> } > > Tangentially, inject uses the first value of the enumerable as the > seed if you don't supply one (the joys of dynamic typing!), so you > can simply say (1..100).inject {|r,v| r*v} This might or might not work, depending on the requirements of the surrounding code: >> [].inject(1){|a,b| a*b} => 1 >> [].inject{|a,b| a*b} => nil >> [].inject(0){|a,b| a+b} => 0 >> [].inject{|a,b| a+b} => nil :-)) If you want to detect the case where there are no elements go without the argument. If you don't care and just want the sum / product provide an argument (the neutral element of the operation). Kind regards robert