[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: object reference handle (like perl's reference to scalar

Eric Mahurin

5/5/2005 6:59:00 PM

> > The original intent was to have a way to modify these
> > references that variables contain. This provides a general
> way
> > of doing this.
>
> Doesn't assignment do that?

Of course. But the code doing this assignment has to know the
name of the variable and the binding(scope) it is in. This
class encapsulates this information and gives get/set methods
to this variable (or thingy).

To not confuse this with "object reference" (which is many
times interchangeable with "object"), you could think of this
thing as a variable/attribute/member/element/thingy reference.
Or even a reference to "object reference". It also corresponds
directly with the phrase "pass by reference". And it maps to
what references/pointers are in other languages.

> > A side effect is that you can do a lot more
> > though - all you need is a way to get and set the thing you
> > want to access.
> >
> > x = ref{:a} # allows you to get/set the reference that :a
> has
> >
> > x[] # get the object that :a has
> > x[]=... # set the object reference that :a has
> >
> > Here is another example (pass by reference):
> >
> > def swap(a,b)
> > tmp = a[]
> > a[] = b[]
> > b[] = tmp
> > end
> >
> > swap(ref{:x},ref{:y}) # swap the object references in x and
> y
> >
> > See how useful this thing is?
>
> I haven't seen any cases where I'd have wanted to have it,
> but I think
> experimentation of this kind is always interesting, even if
> not
> everyone has a direct use for it.

You can always find another way to do this. This just provides
something else in our bag of Ruby tricks. I think it is a nice
generally useful interface.





__________________________________
Do you Yahoo!?
Make Yahoo! your home page
http://www.yaho...


7 Answers

dblack

5/5/2005 8:46:00 PM

0

dave.burt

5/6/2005 2:04:00 AM

0

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

dblack

5/6/2005 2:07:00 AM

0

dave.burt

5/6/2005 5:07:00 AM

0

> Can someone give me an example of a time you need an extra level of
indirection?

A reference can sort of replace itself with another object.

An Integer is immutable, but a reference to an integer can be changed
to any other object.

a = [1, 2, 3].map {|i| i.ref }
a.each {|i|
i[] = 5.1 if i == 2
end

Something analogous to the above might be better (clearer?) than #map
or #each_with_index in some cases.

This also could be used to increase the "mutability" of other objects.
IOs could become StringIOs. Instances of singleton classes could become
instances of normal classes.

class Ref
def revert_type_to_class!
__setobj__(__getobj__.dup)
end
end

And then, how about a dynamic class reference (or other evil variable
constants)?

MyClass = Array.ref
#...
MyClass[] = Hash

Cheers,
Dave

Martin DeMello

5/6/2005 6:03:00 AM

0

David A. Black <dblack@wobblini.net> wrote:
> > #
> > # For those times you need an extra level of indirection.
>
> Can someone give me an example of such a time? I'm still not seeing
> it. (I mean, I get what these things are doing, just not where one
> would want to do them.)

You could have data structures where every member effectively has a
replace() method.

martin

Austin Ziegler

5/6/2005 1:09:00 PM

0

On 5/6/05, dave.burt@gmail.com <dave.burt@gmail.com> wrote:
>> Can someone give me an example of a time you need an extra level
>> of indirection?
> A reference can sort of replace itself with another object.
>
> An Integer is immutable, but a reference to an integer can be
> changed to any other object.
>
> a = [1, 2, 3].map {|i| i.ref }
> a.each {|i|
> i[] = 5.1 if i == 2
> end
>
> Something analogous to the above might be better (clearer?) than
> #map or #each_with_index in some cases.

I disagree. After all, you've had to use map to replace the integers
with Ref-to-integer in the first place.

I still haven't seen any reason that this would be necessary -- or
even useful -- in Ruby. (IMO, John Carter's nil-as-message eater
would be more useful. And I think it's clear how useful I think
*that* is.)

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



Dave Burt

5/6/2005 3:59:00 PM

0

"Austin Ziegler" <halostatue@gmail.com> disabused:

AZ>>> Can someone give me an example of a time you need an extra level
AZ>>> of indirection?

D>> A reference can sort of replace itself with another object.
D>>
D>> An Integer is immutable, but a reference to an integer can be
D>> changed to any other object.

AZ> I disagree. After all, you've had to use map to replace the integers
AZ> with Ref-to-integer in the first place.
AZ>
AZ> I still haven't seen any reason that this would be necessary -- or
AZ> even useful -- in Ruby. (IMO, John Carter's nil-as-message eater
AZ> would be more useful. And I think it's clear how useful I think
AZ> *that* is.)

You're right, you know.

I'm sure this kind of thing would be used occasionally if it was built into
the language (like VB's "ByRef" and "ByVal" or whatever), but even that much
added syntax makes it almost completely useless as far as I can see. Of
course, I'm going to post to the list if I ever find something it
revolutionizes.

Still, variable constants! That don't warn you about it!

Cheers,
Dave