From: Jim Weirich Date: 2005-05-24T13:47:12+09:00 Subject: Re: Array.=== in Rails Bug On Monday 23 May 2005 11:55 pm, Markus wrote: > On Mon, 2005-05-23 at 15:34, Jamis Buck wrote: > > You can add a ticket to dev.rubyonrails.com. That way it won't get > > lost in a shuffle of email. > > Did that, but in the meantime I found a better solution (and included it > in the ticket): > > Change AssociationProxy to "class AssociationProxy < Array" and > let the C code do its thing. No muss, no fuss... FileList in Rake used to inherit from Array to get array-like behavior. I recently changed it to delegate to an internally held array. I found that the delegation allowed a FileList to be used in more places where an Array was expected ... mainly because some methods check for the to_ary method to see if an object should be treated like an array. Given the following code: [FileList['a', 'b'], FileList['c', 'd']].flatten Rake 0.5.0 which inherits from Array will return: [] Rake 0.5.3 which delegates to Array will return: ['a', 'b', 'c', 'd'] That's because flatten accesses the array elements directly. Since FileList is lazy loaded, bypassing the methods means that the elements are never loaded. By switching to a delegation method, flatten checks for :to_ary, which can be used to trigger loading and make everyone happy. Short version: Inheriting from Array is not always the most compatible. Another moral to the story: If you want to check for something that is array like (but doesn't have to be an array), check to see if it responds to to_ary. -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)