From: dave.burt@... Date: 2005-05-06T11:04:29+09:00 Subject: Re: object reference handle (like perl's reference to scalar) 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