[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

object reference handle (like perl's reference to scalar

Eric Mahurin

5/4/2005 9:38:00 PM

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/mai...



4 Answers

Austin Ziegler

5/4/2005 9:55:00 PM

0

On 5/4/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> 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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca



Joel VanderWerf

5/4/2005 10:10:00 PM

0

Eric Mahurin wrote:
> 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


Patrick Hurley

5/6/2005 5:24:00 PM

0

On 5/4/05, Eric Mahurin <eric_mahurin@yahoo.com> wrote:
> 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]



dblack

5/6/2005 5:30:00 PM

0