From: Mark Hubbart Date: 2005-01-20T05:11:46+09:00 Subject: Re: value by reference On Thu, 20 Jan 2005 04:16:02 +0900, Florian Gross wrote: > Mark Hubbart wrote: > > > when you call Ref.new(obj), you get back a Ref object, which will > > appear in every way to be the "obj" that you passed to Ref.new. It > > will give the same id, behave the same, respond to the same methods, > > etc. > > > > Only two differences (that I see): > > First, it responds to two extra methods: __value__ and become. become > > replaces the object that is being referenced, and returns the old > > value. __value__ returns the object being referenced, without the Ref > > wrapper. You should use this method any time you want to serialize or > > Marshal the value. > > Second, it circumvents the public/private method checks, so you can > > call private methods directly on the reference. Sorry. Don't have the > > time to figure out how to fix that without eval. > > Third: > > Ref.new(false) or Ref.new(nil) do not act like you would expect them to > in conditional contexts: > > do_something if ref # always done > x = ref || y # x is always ref Ah, I hadn't thought of that. I guess if you would need to de-reference in conditionals: do_something if ref.__value__ > Fourth: > > Some of Ruby's C methods want their arguments to be instances of a > particular class. They won't even call the usual #to_x methods. > str.scan(ref) is one example of that. I didn't know of that one. So String.scan doesn't use duck typing? > Fifth: > > Object.instance_method(:inspect).bind(ref).call calls the #inspect > method of the ref, not of the Object it refers to. I would consider that a feature :) at a later time, a method could be added, __ref__, would would allow you to call normal ruby methods on the reference object; get its id, inspect it, etc. This was really just a proof-of-concept... and you showed a couple of big holes in it. :) Ahh, well. I suspect that for most things, this will work, with stated caveats. cheers, Mark