From: Tom Sawyer Date: 2003-01-24T13:51:12+09:00 Subject: Re: contained_by method (challange) On Thursday 23 January 2003 04:45 pm, Lyle Johnson wrote: > Congratulations; so few people reach that point in their life ;) only if i knew what i knew better! :-) > > i would like all my objects to have a #contained_by method that returns a > > reference to the object they are contained by. > For starters, you need to clarify what you mean by an object > "containing" another object. > > For your first example, you're talking about one object that is a member > of an Array. For your second example, you're talking about an object > that is an instance variable of another object. Those are two different > concepts. yes, well i figured that #contained_by would have variant meaning dependent on the context, but.... > If you just wanted to focus on the first case -- a method that returns a > list of all the arrays that include the object as an array member -- you > could use this: > > class Object > # Return an array of the arrays that contain me > def contained_by > containers = [] > ObjectSpace.each_object(Array) { |anArray| > containers << anArray if anArray.include? self > } > containers > end > end quite right. an object can be contained by many another object. hmmm.... > Hope this helps, very much so. thank you! -- tom sawyer, aka transami transami@transami.net