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

This looks nice too. I haven't looked at DelegateClass yet,
but it looks kind of like what's in evil.rb/become
(__set_obj__). I would like to add this to my Object.ref which
already does a variety of things.

What you have is definitely different than my earlier thing:

ref {:x} # refers to the variable x
x.ref # refers to object in the variable x

example:

x = "hello"
y = "world"
z = x
swap(ref{:x},ref{:y}) # swaps what x and y point to
x -> "world"
y -> "hello"
z -> "hello"

x = "hello"
y = "world"
z = x
swap(x.ref,y.ref) # swaps the objects in x and y
x -> "world"
y -> "hello"
z -> "world"

Using ref{...} also allows you get/set about anything that
looks like a variable assignment (i.e. attributes,members,,
etc).

I also want to keep my base reference class to work on just
getter and setter procs because that is the most basic. So,
you could be crazy and do something like this:

line = Reference.new(proc{gets},proc{|*a|puts(*a)})

line # equivalent to gets
line="hello" # equivalent to puts("hello")

Of course derived classes of Reference could do just about
anything they want for "" and "=".

···

--- dave.burt@gmail.com wrote:

To me, this looks more Rubyish. Is there some benefit I'm
missing in
your pulling variable names out of the closure like you do
there, Eric?
If it's all the same, why not just have the object pass
itself into a
reference constructor (as my Object#ref does)?
# ref.rb
#
# For those times you need an extra level of indirection.
#
# Example:
# def swap(a,b)
# tmp = a
# a = b
# b = tmp
# end
#
# swap(x.ref, y.ref) # convert x and y to references then
swap them
#
Ref = DelegateClass(Object) unless Object.const_defined? :Ref
class Ref
  # Dereference (get the referred-to value)
  alias deref __getobj__
  # Call with no index to dereference
  def (*args)
    if args.empty?
      __getobj__
    else
      super
    end
  end
  # Set value (change the reference to point at something
else)
  def ref=(val) __setobj__(val) end
  # Call with no index to assign a new value
  def =(*args)
    if args.size == 1
      __setobj__(args[0])
    else
      super
    end
  end
end
class Object
  # Make a reference
  def ref
    Ref.new(self)
  end
end

Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mailtour.html

Hi --

···

On Fri, 6 May 2005, Eric Mahurin wrote:

This looks nice too. I haven't looked at DelegateClass yet,
but it looks kind of like what's in evil.rb/become
(__set_obj__). I would like to add this to my Object.ref which
already does a variety of things.

What you have is definitely different than my earlier thing:

ref {:x} # refers to the variable x
x.ref # refers to object in the variable x

example:

x = "hello"
y = "world"
z = x
swap(ref{:x},ref{:y}) # swaps what x and y point to
x -> "world"
y -> "hello"
z -> "hello"

This is uncomfortably (for me) close to all the symbolic reference
stuff one sees in Perl. It seems similarly verbose and fragile. I
don't think it's even looked upon very favorably in Perl circles. In
any case I much prefer:

   x,y = y,x

David

--
David A. Black
dblack@wobblini.net