From: "David A. Black" Date: 2008-03-30T16:14:21+09:00 Subject: Re: inject's pathological case... Hi -- On Sun, 30 Mar 2008, Just Another Victim of the Ambient Morality wrote: > I'm not sure how I feel about inject's pathological case. What do you > all think should happen in the following code? > > > [2].inject {|a, i| puts i} > > > I might have thought that the block would never be called and the method > would return 2. > If the block must be called, I might expect "i" to be nil. > What actually happens is that the block is called and "i" is the same > thing as "a". I did not expect this and I'm trying to figure out how this > is either consistent or useful. Can someone clue me in to this behaviour? > Thank you... Actually the block isn't being called: irb(main):005:0> [2].inject {|a, i| puts "hi" } => 2 irb(main):006:0> [2].inject {} => 2 The value of the whole statement is 2, or, more generally, the first (and only) element in the enumerable. I guess that's all it can do since it doesn't have enough elements to call the block even once. David -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS April 14-17 New York City INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin See http://www.rubypal.com for details and updates!