#to_ary behavior does not conform to MRI behavior #3377

Closed
asthasr opened this Issue Oct 8, 2015 · 2 comments

Comments

Projects
None yet
4 participants
@asthasr

asthasr commented Oct 8, 2015

In MRI Ruby, when you iterate over an object providing a block with extra parameters, it seems that #to_ary is called and, if it returns nil, is disregarded. This is a test that you can execute to see the behavior on different Rubies:

class Example
  def to_ary
    nil
  end
end

test = [Example.new].map { |x, _| x }

puts test == [nil] ? "fail" : "pass"

On MRI versions, this passes. On JRuby 1.7.22, it returns [nil] and thus fails. On JRuby 9.0.1.0, it raises an exception: TypeError: Example#to_ary should return Array. It seems that the behavior in this case should conform to the behavior on MRI, in order to avoid surprising behavior when attempting to use map, each and other enumerable methods.

In real projects, this is observable on any ActiveRecord object as well as DelayedJob, as seen in Issue #3338.

@headius

This comment has been minimized.

Show comment
Hide comment
@headius

headius Oct 8, 2015

Member

Ahh phooey...just when I think we've got to_ary working exactly right, some other case comes up. Will investigate.

Member

headius commented Oct 8, 2015

Ahh phooey...just when I think we've got to_ary working exactly right, some other case comes up. Will investigate.

@enebo

This comment has been minimized.

Show comment
Hide comment
@enebo

enebo Oct 8, 2015

Member

I will fix this as part of 1.7.x but the fix for this was done earlier in a commit I noted in #3338.

Member

enebo commented Oct 8, 2015

I will fix this as part of 1.7.x but the fix for this was done earlier in a commit I noted in #3338.

@enebo enebo added this to the JRuby 1.7.23 milestone Oct 8, 2015

@enebo enebo closed this Oct 8, 2015

@kares kares added the JRuby 1.7.x label Nov 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment