Thank you everyone. I would like thank every one one by one, but I think
that will make this place look like a blog and I'm not sure if that's
proper to this forum. But I really appreciate everyone's support.
Creating a class variable, or using weak references (though still
complicated to me) made very much sense. The code at
http://pastie.org/410782 was clear to the point where a @name instance
variable is added to the class.
After cramming all this code into by head, I started to wonder if I was
able to do what I wanted to by using the #object_id? I'm not sure at
this point. Below is an example of what I wanted to achieve.
What exactly do you mean by "actual identifier"? If you mean #object_id
then there is method ObjectSpace._id2ref.
I meant the variable that I used to point to the object, when they are
created like:
var = Foo.new(77)
I wanted to get access to 'var' by using it's @some_id which is 77.
The question is: what are you trying to achieve?
Thank you for asking. I was wondering how to update relations(imagine a
social networking site) between objects of a single Class. I hope this
will clarify why I want to access the variable of the object with its
instance variable. This how far I got last night:
class Foo
attr_accessor :name, :some_id, :relations
def self.update_relationship(foo1, foo2)
# code that adds @relations for foo1, and foo2 and updates it with
# it's own MINUS its own :some_id, and updates other @some_id's
included
# in foo1.relations + foo2.relations.
end
def self.find_by_some_id(some_id)
found = nil
ObjectSpace.each_object(Foo) { |o| found = o if o.oid == oid}
found
end
def to_s
@name
end
end
(create two objects):
John = Foo.new('John', 1,)
Dave = Foo.new('Dave', 2,)
(create a relationship between the two):
Foo.update_relationship(John, Dave)
John.relations -> [2]
Dave.relations -> [1]
Let's assume that along the line more objects are created and a new
relationship is created with John.(I'm just going to create a new object
and populate it's @relations instance variable, so the variables are
visible)
Steve = Foo.new('Steve', 104, [55, 77])
Foo.update_relationship(Steve, John)
John.relations -> [2, 55, 77, 104]
Steve.relations -> [1,2,55,77]
...well, that's good, but all the other objects (with @some_id 2, 55 and
77) would have to be updated right? For starters, Dave, the second
object (with @some_id = 2) is currently still:
Dave.relations -> [1]
Where it has to be updated to [1, 55, 77, 104]
So in order to access the Dave object, I only had 2, it's @some_id
instance variable to access it. Now, since I included the variable name
in the instance variable I can access the Dave object with the
find_by_some_id class method, and
get the @name instance variable which is the same as the variable (THIS
IS WHERE I THOUGHT IT WAS NOT A RUBYIST SOLUTION)
I'm going to read a little more and see if I can use the object's
#object_id to do all of this.
Thank you.
Aki
···
--
Posted via http://www.ruby-forum.com/\.