Project

General

Profile

Actions

Backport #1962

closed

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

Added by oggy (George Ogata) over 15 years ago. Updated over 5 years ago.


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
Actions

Also available in: Atom PDF

Like0
Like0Like0