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#take duplicates objects #6036

Closed
palkan opened this issue Jan 13, 2020 · 1 comment
Closed

Enumerator#take duplicates objects #6036

palkan opened this issue Jan 13, 2020 · 1 comment

Comments

@palkan
Copy link

@palkan palkan commented Jan 13, 2020

Environment

Provide at least:

  • JRuby version: jruby 9.2.9.0 (2.5.7) 2019-10-30 458ad3e OpenJDK 64-Bit Server VM 25.232-b09 on 1.8.0_232-b09 +jit [linux-x86_64]
  • Operating system and platform (e.g. uname -a): Linux 4b8f03f66801 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 GNU/Linux

Expected Behavior

Expected Enumerator#take behavior to match MRI (2.6).

a = []

init = {a: 1, b: 1}

block = ->(obj){ a << obj; obj.shift if obj.respond_to?(:shift) }

enum = Enumerator.new do |y|
  val = init

  loop do
    y << val
    val = block.(val)
  end
end

res =  enum.take(4)
p res  #=>[{:b=>1}, [1], :a, nil]

(the example is take from MRI tests: https://github.com/ruby/ruby/blob/a65e8644fb97491314387e4138cabf6378a8e8d5/test/ruby/test_enumerator.rb#L836)

Actual Behavior

In JRuby it prints [{:b=>1}, [:a, 1], :a, nil].

If we add some puts-debugging, it seems that the value passed to y (or taken?) is duplicated — it has a different object_id:

a = []

init = {a: 1, b: 1}

block = ->(obj){ a << obj; p obj.object_id if obj == [:a, 1]; obj.shift if obj.respond_to?(:shift) }

enum = Enumerator.new do |y|
  val = init

  loop do
    y << val
    val = block.(val)
  end
end

res = enum.take(4)

p res[1].object_id

# 2000
# 2002
@palkan palkan changed the title Enumerator#take duplicate objects Enumerator#take duplicates objects Jan 13, 2020
@headius headius added this to the JRuby 9.2.10.0 milestone Jan 13, 2020
@headius
Copy link
Member

@headius headius commented Jan 13, 2020

Confirmed on 9.2.9:

$ rvm jruby-9.2.9.0 do jruby test.rb
[{:b=>1}, [:a, 1], :a, nil]

But it seems to work correctly on 9.2.10:

$ jruby -v test.rb
jruby 9.2.10.0-SNAPSHOT (2.5.7) 2020-01-10 916a77e OpenJDK 64-Bit Server VM 25.222-b10 on 1.8.0_222-b10 +jit [darwin-x86_64]
[{:b=>1}, [1], :a, nil]

And the object_id in the second script also matches:

$ jruby test.rb
2000
2000

Probably fixed by @kares here: c7bd47c

@headius headius closed this as completed Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants