From: Dave Burt Date: 2005-05-07T21:44:23+09:00 Subject: Re: object reference handle (like perl's reference to scalar) "Eric Mahurin" : > Where do I get your ref.db? I saw a link in another post of > yours and it didn't work. Sorry, my fault for posting a link that didn't exist yet. http://www.dave.burt.id.au/ruby/ref.rb is there now. It may disappear when I become sufficiently ashamed of it. > > What does this do? > > # silly example as x,y = y,x would be better > ... C:\>ruby require 'ref' def swap(a,b) tmp = a[] a[] = b[] b[] = tmp end x = 1.ref y = 2.ref swap(x, y) p x p y x = "hi".ref y = "world".ref z = x swap(x, y) p x p y p z ^Z 2 1 "hi" "world" "hi" > If this doesn't work, I don't see the real use of the way you > did it. If on the other hand you use evil.rb/become or replace > to assign the dereferenced object, you could at least get the > second case to work (and other non-immediates). If it does > work, great. I want to know how the immediate case is handled. I'm not being evil. My Ref is just an extra wrapper for an object. > For now, I'm using replace or become (if available to the > object) to accomplish the above dereferencing (to an object) > assignment. And I still have all the other types of references > (to a variable, assignable expression, attribute/member, > get/set methods, etc) in place. I need to document what I > have, package it into a gem and upload to rubyforge. > > I really don't like the idea of your references looking like > the underlying object. This just confuses things. A reference > is a reference and you can get/set the object that it > references. If you want to get info about the object that it > references, just get it and query it by getting it from the > reference. Why muddy what the reference is? You're right, there's no real advantage to be gained here. You might as well use Ref = Struct.new(:deref) That provides a wrapper for an object - just treat it as if it's name ends in ".deref": def swap(a, b) a.deref, b.deref = b.deref, a.deref end Cheers, Dave