> 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.yahoo.com/r/hs
Hi --
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.
I don't think I'm confusing it. variable/attribute/...thingy confuses
me, though, so maybe that counts
It calls to mind the Perl
"underlying thingy" concept, which I think is a reasonable way to
explain objects in Perl but not necessarily something I'd want to
emulate in Ruby. As you say, one would never literally need it. But
I'd still be interested in seeing cases where it would add clarity and
structure to a design. (Yes, you detect a note of skepticism
But
it's all academic, as they say.)
David
···
On Fri, 6 May 2005, Eric Mahurin wrote:
--
David A. Black
dblack@wobblini.net
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
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
You could have data structures where every member effectively has a
replace() method.
martin
···
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.)
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
···
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.
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
"Austin Ziegler" <halostatue@gmail.com> disabused:
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.
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.)
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