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

Discussion in 'Ruby' started by Eric Mahurin, May 4, 2005.

  1. Eric Mahurin

    Eric Mahurin Guest

    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
     
    Eric Mahurin, May 4, 2005
    #1
    1. Advertising

  2. On 5/4/05, 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).


    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 *
    * Alternate:
     
    Austin Ziegler, May 4, 2005
    #2
    1. Advertising

  3. 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
     
    Joel VanderWerf, May 4, 2005
    #3
  4. On 5/4/05, 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
    >


    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]
     
    Patrick Hurley, May 6, 2005
    #4
  5. Hi --

    On Sat, 7 May 2005, Patrick Hurley wrote:

    > On 5/4/05, 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
    >>

    >
    > 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

    --
    David A. Black
     
    David A. Black, May 6, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf Wahner
    Replies:
    2
    Views:
    1,113
    Ralf Wahner
    May 28, 2004
  2. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
  3. Clint Olsen
    Replies:
    6
    Views:
    362
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    164
    Arndt Jonasson
    Jan 27, 2005
  5. Bryan
    Replies:
    1
    Views:
    77
Loading...

Share This Page