Project

General

Profile

Backport #1962

Kernel#Array shouldn't call #to_a if it has been undef'd

Added by oggy (George Ogata) almost 10 years ago. Updated 7 days ago.

Status:
Closed
Priority:
Normal
[ruby-core:24988]

Description

=begin
Applies to 1.8.*.

Kernel#Array is supposed to behave as follows:

  • if the object has a #to_ary, call it and return the result
  • else if the object has a #to_a, call it and return the result
  • else return the object in a 1-element array

If #to_a has been undefined (with #undef_method), however, #Array will see the NULL entry in the method table and try to call it. This is silly. Practically speaking, this leads to spurious warnings ("default `to_a' will be obsolete") when calling #Array on an ActiveRecord AssociationProxy. This happens because AssociationProxy undefs all its methods (including #to_a, since Object#to_a is defined), in order to delegate everything to the object it wraps via #method_missing. #Array will thus find #to_a to be mapped to an undef'd method, and proceed to call it, which gets delegated to the wrapped object, which doesn't implement #to_a, so it falls back to Object#to_a -- hence the warning.

Aside from finding this behavior strange, I don't really see how ActiveRecord can work around this properly. I've attached a patch which makes #Array check if the #to_a it found is an undef'd method, and not call it if it is. This is consistent with 1.9.
=end


Files

Array.patch (609 Bytes) Array.patch oggy (George Ogata), 08/20/2009 02:51 PM

History

#1

Updated by naruse (Yui NARUSE) over 9 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

#2

Updated by jeremyevans0 (Jeremy Evans) 7 days ago

  • ruby -v deleted (ruby 1.8.6 (2009-08-19 patchlevel 384) [i686-darwin9.7.0])
  • Status changed from Assigned to Closed
  • Description updated (diff)
  • Project changed from Ruby 1.8 to Backport187
  • Tracker changed from Bug to Backport

Also available in: Atom PDF