![]() |
object reference handle (like perl's reference to scalar)
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 |
Re: object reference handle (like perl's reference to scalar)
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 |
Re: object reference handle (like perl's reference to scalar)
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 |
Re: object reference handle (like perl's reference to scalar)
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] |
Re: object reference handle (like perl's reference to scalar)
Hi --
On Sat, 7 May 2005, Patrick Hurley wrote: > 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] How about: a = b = c = [1,2,3] c[1] = 7 p a # [1, 7, 3] :-) David -- David A. Black dblack@wobblini.net |
| All times are GMT. The time now is 08:39 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.