Enumerator#inject() is broken #941

Closed
marques-work opened this Issue Aug 2, 2013 · 1 comment

Projects

None yet

2 participants

@marques-work

I've only tested the inject() method, but other methods may be broken. I've tested this in both 1.8 and 1.9 modes. Please see examples below:

def broken_inject
  a = %w(1 2 3 4 5 6 7 8 9 10)
  keys = %w(a b c)

  b = a.each_slice(4).inject({}) do |res, c|
    res[keys.shift] = c
    res
  end
end

def working_inject
  a = %w(1 2 3 4 5 6 7 8 9 10)
  keys = %w(a b c)

  # forcing the enumerator to an array allows inject to work as expected
  b = (a.each_slice(4)).to_a.inject({}) do |res, c|
    res[keys.shift] = c
    res
  end
end

broken_inject
# => {"a"=>["9", "10"], "b"=>["9", "10"], "c"=>["9", "10"]}

working_inject
# => {"a"=>["1", "2", "3", "4"], "b"=>["5", "6", "7", "8"], "c"=>["9", "10"]}
@BanzaiMan
Member

Fixed on master.

irb(main):001:0> RUBY_DESCRIPTION
=> "jruby 1.7.5.dev (1.9.3p392) 2013-08-03 0e02a86 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_25-b15 [darwin-x86_64]"
irb(main):002:0> def broken_inject
irb(main):003:1>   a = %w(1 2 3 4 5 6 7 8 9 10)
irb(main):004:1>   keys = %w(a b c)
irb(main):005:1> 
irb(main):006:1*   b = a.each_slice(4).inject({}) do |res, c|
irb(main):007:2*     res[keys.shift] = c
irb(main):008:2>     res
irb(main):009:2>   end
irb(main):010:1> end
=> nil
irb(main):011:0> broken_inject
=> {"a"=>["1", "2", "3", "4"], "b"=>["5", "6", "7", "8"], "c"=>["9", "10"]}
@BanzaiMan BanzaiMan closed this Aug 4, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment