Backport #1962

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

Added by oggy (George Ogata) almost 11 years ago. Updated 12 months ago.



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.


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

Updated by naruse (Yui NARUSE) over 10 years ago

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




Updated by jeremyevans0 (Jeremy Evans) 12 months 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