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

E

Eric Mahurin

In ruby, is there a way to get a handle of an object reference?
In perl, this is the \ operator:

$x = 1; # \$x is a handle to change $x
$a = [1,2,3]; # \$a->[1] is a handle to change an element in $a

As far as I can tell, the closest that Ruby has to this is a
symbol. But, this only works for object references that have
an associated variable name. For example, there is no symbol
associated with an element of an array (or hash).




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

Austin Ziegler

In ruby, is there a way to get a handle of an object reference? In
perl, this is the \ operator:

$x = 1; # \$x is a handle to change $x
$a = [1,2,3]; # \$a-> [1] is a handle to change an element in $a

As far as I can tell, the closest that Ruby has to this is a
symbol. But, this only works for object references that have an
associated variable name. For example, there is no symbol
associated with an element of an array (or hash).

What are you trying to do? There is no equivalent to what you want
in Ruby, but in most cases, it's not necessary. A little bit of
rethinking, on the other hand, is necessary.

There is no way to do the following:

a = [1, 2, 3]
x = a[1]
x = 4 # a == [1, 4, 3]

Variables in Ruby are simply labels for object references. They are
not objects themselves. In Ruby, variables are transient names --
they are effectively "weightless" and take up no space
(effectively). In Perl and C-like languages, variables themselves
take up space on the call stack and may refer to other places on the
heap.

-austin
 
J

Joel VanderWerf

Eric said:
In ruby, is there a way to get a handle of an object reference?
In perl, this is the \ operator:

$x = 1; # \$x is a handle to change $x
$a = [1,2,3]; # \$a->[1] is a handle to change an element in $a

As far as I can tell, the closest that Ruby has to this is a
symbol. But, this only works for object references that have
an associated variable name. For example, there is no symbol
associated with an element of an array (or hash).

You can do something like this with closures:

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> set_a_1, get_a_1 = proc {|v| a[1]=v}, proc {a[1]}
=> [#<Proc:0x401e8a6c@(irb):2>, #<Proc:0x401e89b8@(irb):2>]
irb(main):003:0> set_a_1[5]
=> 5
irb(main):004:0> a
=> [1, 5, 3]
irb(main):005:0> get_a_1[]
=> 5

Note that if the binding of a changes, then set_a_1 and get_a_1 refer to
the new value. If you want the two procs always to refer to the same
array, you need to introduce a new variable (probably better be in a new
scope, as well):

irb(main):009:0> def make_elt_1_refs(x)
irb(main):010:1> [proc {|v| x[1]=v}, proc {x[1]}]
irb(main):011:1> end
=> nil
irb(main):012:0> set_1, get_1 = make_elt_1_refs(a)
=> [#<Proc:0x401f6f18@(irb):10>, #<Proc:0x401f6e00@(irb):10>]
irb(main):013:0> a = []
=> []
irb(main):014:0> get_1[]
=> 5
 
P

Patrick Hurley

In ruby, is there a way to get a handle of an object reference?
In perl, this is the \ operator:

$x = 1; # \$x is a handle to change $x
$a = [1,2,3]; # \$a->[1] is a handle to change an element in $a

Not the "ruby way," but how about:

a = [1, 2, 3]
b = a.object_id
c = ObjectSpace._id2ref(b)
c[1] = 7
puts a => [1, 7, 3]
 
D

David A. Black

Hi --

In ruby, is there a way to get a handle of an object reference?
In perl, this is the \ operator:

$x = 1; # \$x is a handle to change $x
$a = [1,2,3]; # \$a->[1] is a handle to change an element in $a

Not the "ruby way," but how about:

a = [1, 2, 3]
b = a.object_id
c = ObjectSpace._id2ref(b)
c[1] = 7
puts a => [1, 7, 3]

How about:

a = b = c = [1,2,3]
c[1] = 7
p a # [1, 7, 3]

:)


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top