From: 7stud -- Date: 2008-03-30T15:21:20+09:00 Subject: Re: inject's pathological case... Just Another Victim... 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? I'm not seeing that: 1) Two element array: result = [2, 3].inject do |a, i| puts "a:#{a}" puts "i:#{i}" puts "hello" puts "--------" end puts "result=#{result}" if result.nil? puts 'yes' end --output:-- a:2 i:3 hello -------- result= yes 2) One element array: result = [2].inject do |a, i| puts "a:#{a}" puts "i:#{i}" puts "hello" puts "--------" end puts "result=#{result}" if result.nil? puts 'yes' end --output:-- result=2 -- Posted via http://www.ruby-forum.com/.