From: Eric Mahurin Date: 2005-05-06T22:15:31+09:00 Subject: Re: 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