From: Geert Fannes Date: 2006-03-09T19:47:10+09:00 Subject: how to introduce reference objects into ruby ------_=_NextPart_001_01C64366.BC7FF439 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hello, =20 One thing that hinders the usability of Ruby for the things I want to do, is the fact that certain objects cannot be modified "in place" and always return NEW objects (like Fixnum, Bignum or Float), making it hard to do parameter passing by reference or similar things. Things get easily out of sync and both memory and cpu time (and programming overhead) has to be wasted to keep things going as should. =20 To make things clear: Suppose I have a certain number distributed over different objects and I want this number to be synchronized (read "the same") in these different places: =20 a=3Db=3D12 #a and b are my to objects I want to be synchronized a+=3D1 p a #=3D> 13 p b #=3D> 12 KO :-( I want it to be 13, exactly the same as a =20 I can of course capture this number in a class, and distribute the class object instead of the number object: =20 class Bla attr :number,true end (a=3Db=3DBla.new).number=3D12 a.number+=3D1 p a.number #=3D> 13 p b.number #=3D> 13 OK! :-) =20 But now my code looks more complicated, I have an extra class and have to remember to use the instance variable .number. This also wastes too much memory when e.g. implementing a matrix object where I want to have both an array holding the rows, one for holding the columns and the actual rows and columns SHARE the same numbers using objects of type Bla. This way, if I modify a certain element (say element at row i, column j) by taking the i-th row and modifying the j-th element in that row, I want to see the change also if I access the same matrix element by selecting the j-th column and the i-th element from that column. =20 I tried to create a class that contains a certain Ruby object and that passes all the incoming methods to the contained object, acting as a kind of reference. =20 class Reference attr :containedObject,true =20 def initialize(obj) @containedObject=3Dobj end =20 #handle missing methods and pass them to the contained object def method_missing(methodSymbol,*args) @containedObject.send(methodSymbol,*args) end =20 #override some general existing methods, to make sure that a Reference object is as stealth as possible def class @containedObject.class end #unfortunately, this fails since assignment cannot be overloaded, which makes sense, but still ... def =3D(rhs) @containedObject=3Drhs end end =20 Unfortunately, I cannot overload the assignment operator (and some other operators), so I still cannot work with these objects as if I was working with a normal number. So my guess is that to implement such a Reference class, I have to go deeper and dive in the Ruby code itself, modifying the unoverloadable operators in the parser etc. =20 This seems a bit drastic, if someone knows an easier solution to this problem, please let me know. If not, are there people willing to help me with introducing a Reference object in Ruby or have suggestions how this can be done? I cannot imagine no one encountered the same problems before. =20 Greetings, Geert. ------_=_NextPart_001_01C64366.BC7FF439--