Object reference handle (like perl's reference to scalar)

"Eric Mahurin" <eric_mahurin@yahoo.com> queried:

>> (ref.rb)

>I tried this out and it doesn't seem to do anything. I did
a
> require "delegate" followed by the code below. Could you
give
> a concrete example - working on mutable (string or array)
and
> immediate obects (i.e. Fixnum). Even if you get this to
work
> on the mutable objects, I don't see it working on the
immediate
> objects.

# require 'ref'

irb(main):030:0> a = 1.ref
=> 1
irb(main):031:0> a.ref = 2 # = is a synonym for ref=
=> 2
irb(main):032:0> a # here you can see the ref delegates
inspect to the
number 2
=> 2
irb(main):033:0> a + 1 # and + is delegated too
=> 3
irb(main):034:0> a.class # class, kind_of?, is_a? and
respond_to? aren't
=> Ref
irb(main):035:0> a.deref.class # is a synonym for deref
=> Fixnum

Where do I get your ref.db? I saw a link in another post of
yours and it didn't work.

What does this do?

# silly example as x,y = y,x would be better
def swap(a,b)
    tmp = a
    a = b
    b = tmp
end
x = 1
y = 2
swap(x.ref,y.ref)
p x
p y
x = "hi"
y = "world"
z = x
swap(x.ref,y.ref)
p x
p y
p z

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.

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.

Maybe ref= should be called deref=. But there it is.

yes, this is a dereferencing modify. You are not modifying the
reference itself.

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?

···

--- Dave Burt <dave@burt.id.au> wrote:

__________________________________
Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.
http://mobile.yahoo.com/learn/mail

Because no one else is going to write their libraries to check for
reference holders.

Which means that the use of a Ref is, at best, extremely limited.

I won't personally use a library that requires that I use a Ref, as
I find it a concept that is alien to Ruby -- and introduces a really
ugly syntax, to boot. (The C/C++ &foo, *foo, foo->bar looks a lot
cleaner by comparison than foo.bar)

-austin

···

On 5/7/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:

> Maybe ref= should be called deref=. But there it is.
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?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

"Eric Mahurin" <eric_mahurin@yahoo.com>:

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