From: Florian Gross Date: 2005-01-20T04:16:02+09:00 Subject: Re: value by reference 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 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. Fifth: Object.instance_method(:inspect).bind(ref).call calls the #inspect method of the ref, not of the Object it refers to.