From: Just Another Victim of the Ambient Morality Date: 2008-03-31T05:05:08+09:00 Subject: Re: inject's pathological case... "David A. Black" wrote in message news:Pine.LNX.4.64.0803300208460.27343@rubypal.com... > 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. D'oh! I was confused by IRB's interface, for no good reason. I'll put the crack pipe down, now. It's doing exactly what I thought it should do...