okay, after all this time i finally figured out exactly what i am after.
i would like all my objects to have a #contained_by method that returns a
reference to the object they are contined by.
for example:
a = "1"
ar = [a]
a.contained_by # --> reference to ar
another:
class T
attr_reader :a
end
t = T.new
t.a.contained_by # --> reference to t
and so on.
is this possible?
···
–
tom sawyer, aka transami
transami@transami.net
Tom Sawyer wrote:
okay, after all this time i finally figured out exactly what i am after.
Congratulations; so few people reach that point in their life 
i would like all my objects to have a #contained_by method that returns a
reference to the object they are contained by.
for example:
a = “1”
ar = [a]
a.contained_by # → reference to ar
another:
class T
attr_reader :a
end
t = T.new
t.a.contained_by # → reference to t
and so on.
is this possible?
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.
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
Hope this helps,
Lyle
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!
···
On Thursday 23 January 2003 04:45 pm, Lyle Johnson wrote:
–
tom sawyer, aka transami
transami@transami.net
Doesn’t quite work:
a = “Hello”
b = “Hello”
c = [a]
c.include?(b) #=> true
martin
···
Lyle Johnson lyle@users.sourceforge.net wrote:
ObjectSpace.each_object(Array) { |anArray|
containers << anArray if anArray.include? self
}
Martin DeMello wrote:
Doesn’t quite work:
a = “Hello”
b = “Hello”
c = [a]
c.include?(b) #=> true
Good catch; I had forgotten that Array#include? tests for object
equality (==) instead of identity. I guess something like this is better:
def contained_by
containers = []
ObjectSpace.each_object(Array) { |anArray|
firstMatch = anArray.detect { |obj| obj.equal? self }
containers << anArray unless firstMatch.nil?
}
containers
end
– Lyle