From: Robert Klemme Date: 2009-03-09T06:53:25+09:00 Subject: Re: Can I access or find a object from it's instance variable? On 08.03.2009 19:01, Christopher Dicely wrote: >>> 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. You can't really as this is a local variable and you would have to know the scope (or binding) where to search - the name alone is not sufficient. However, for the problem you are trying to solve this is not necessary. >>> 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. > Note that because Ruby objects are passed as references, you generally > don't need to keep relationships using an identifier, you can just > keep a list of the "related" objects in an instance variable (the code > becomes less clear when you store an identifier instead of the object > itself, you probably aren't saving space, and you are probably hurting > performance by necessitating a method call to lookup the actual object > from the identifier.) Adding to that, Aki, if you need to maintain relationships more often then you could follow a similar approach as has been taken with attr_accessor and write methods that generate the code for relationship maintenance. E.g. class Module def one_to_one(name, cl) module_eval %Q{ def #{name}=(x) # logic to set relationship consistently end def #{name} @#{name} end } cl.module_eval %Q{ # similar for the other side } end end and then class Foo one_to_one :parent, Foo end I guess something like this does exist already. You can check the RAA. If you use ActiveRecord for persistence you get these things for free. Kind regards robert