From: Jamis Buck Date: 2005-05-24T05:21:06+09:00 Subject: Re: Array.=== Bug, Rails Bug, or brain failure? Markus, when x.has_many :y, the :y property is not an array. It's an object that walks and talks like an array. (Just try using x.y.find sometime--it doesn't work like the Array version, which bit me hard several times in the past.) If you _need_ it to be an array, you can do x.y.to_a. Otherwise, it's all about duck typing. :) - Jamis On May 23, 2005, at 2:04 PM, Markus wrote: > I've got some rails code that is failing in a very strange way. It is > quite repeatable in situ, but I have yet to formulate a simple > explanation of when it fails. > > Consider this code: > > case x > when Array > print "Array\n" > else > print "Bogus\n" if x.is_a? Array > end > > Should it ever print "Bogus\n"? I wouldn't think so, but when x > is the > Array returned by main_record.sub_records (where main_record is linked > to sub_records by a "has_many :sub_record") it does. > > The class of x is "Array" and (obviously) "x.is_a? Array" but > "Array === > x" returns false. It works for hand made arrays, the Array > returned by > "SubRecord.find_all_by_main_record(main_record.id)"--which should be > identical. > > I've only found one place in the whole codebase (in activerecord) > where > === is redefined, and then only for active records themselves, not for > the class, and certainly not for Class. In any case, it "redefines" it > as "other.is_a?(self)", which is what I'd expect class to do. > > Adding: > > class Array > def self.===(other) > other.is_a? self > end > end > > right above the code in question fixes it. > > Has anyone else seen this? Did the semantics change when I wasn't > looking? Or is there a bug somewhere? Or am I missing something > subtly > obvious? > > --MarkusQ > > > > >