Matthew Sadler created JRUBY-6404:
-------------------------------------
Summary: multiple assignment fails to deconstruct object
Key: JRUBY-6404
URL: https://jira.codehaus.org/browse/JRUBY-6404
Project: JRuby
Issue Type: Bug
Affects Versions: JRuby 1.6.6, JRuby 1.7
Reporter: Matthew Sadler
Assignee: Thomas E Enebo
Priority: Minor
Attachments: jruby_multiple_assignment_bug.rb
The behaviour of multiple assignment doesn't match MRI.
Firstly, when performing multiple assignment JRuby fails to call #to_ary on the
object on the right of the assignment if the object implements the handler for
:to_ary in #method_missing, and is using the #respond_to_missing? callback for
correct #respond_to? behaviour.
If that's not very clear, hopefully the following code makes more sense:
class ArrayWrapper
def initialize(ary)
@ary = ary
end
def method_missing(name, *args, &block)
@ary.send(name, *args, &block)
end
def respond_to_missing?(name, include_private=false)
@ary.respond_to?(name, include_private)
end
end
wrapper = ArrayWrapper.new([1, 2, 3])
wrapper.respond_to?(:to_ary)
#=> true, via #respond_to_missing?, ruby 1.9 only
a, b, c = wrapper
# would expect a #=> 1, b #=> 2, c #=> 3,
# but instead a is wrapper, b and c are nil
This works however if the correct #method_missing? behaviour is implemented by
overriding #method_missing?, rather than relying on the #respond_to_missing?
callback, which is a little strange.
Secondly, MRI actually always calls #to_ary, regardless of what #responds_to?
says, eg, the following works in MRI:
class ArrayWrapper
def initialize(ary)
@ary = ary
end
def method_missing(name, *args, &block)
@ary.send(name, *args, &block)
end
end
a, b, c = ArrayWrapper.new([1, 2, 3])
# a #=> 1, b #=> 2, c #=> 3
Obviously fixing this second issue will resolve the first, but I thought it was
worth raising the first in case it revealed further issues with #respond_to?
tests.
I've attached a test case that passes under MRI and fails under JRuby.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://jira.codehaus.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email