Reputation: 8434
I am currently trying to create an engine for a new application.
Design is quite complex and i realy need a way to create a reference or a pointer or what ever way to remotely modify and access the datas. Datas must be kept in a single place and must not be duplicated.
Is there a way do get an object by reference ? For example can I get the instance of a class with his object_id ?
Thanks for your help ;)
Upvotes: 0
Views: 189
Reputation: 5717
The following should demonstrate how to get a reference to an object and de-reference it:
s = "I am a string" #=> "I am a string"
r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
r == s #=> true
You may wish to review the documentation for Object and ObjectSpace.
Upvotes: 1
Reputation: 79562
Not sure exactly what you want to achieve. Ruby uses references for all custom objects already.
You may want to look at the delegate
library.
require 'delegate'
obj = "foo"
ptr = SimpleDelegator.new(obj)
ptr.upcase # => "FOO"
other_obj = "bar"
ptr.__setobj__(other_obj) # make ptr point to another object
Upvotes: 0