Skip to content

Enumerator#inject() is broken #941

@marques-work

Description

@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"]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions