Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerator#inject() is broken #941

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

Enumerator#inject() is broken #941

marques-work opened this issue Aug 2, 2013 · 1 comment
Labels
Milestone

Comments

@marques-work
Copy link

@marques-work marques-work commented Aug 2, 2013

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
Copy link
Member

@BanzaiMan BanzaiMan commented Aug 4, 2013

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
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants