From: William Djaja Tjokroaminata Date: 2002-08-01T04:19:49+09:00 Subject: Re: How to Emulate C/C++ Pointer/Reference in Ruby? Well, probably for the temporary variable it is not very useful to have a "reference to a", but for an object attribute it may make more sense. Because I am in the middle of playing around with Ruby C API, do you think it will be beneficial to create my own type of class of reference? It may be something like this. cReference = rb_define_class ("Reference", rb_cObject); static VALUE reference_init (VALUE self, VALUE obj, char* name) { rb_iv_set (self, "@obj", obj); rb_iv_set (self, "@name", name); return self; } reference_read (VALUE self) { return rb_iv_get (rb_iv_get (self, "@obj"), STR2CSTR (rb_iv_get (self, "@name"))); } rb_define_method (cReference, "initialize", reference_init, 2); rb_define_method (cReference, "value", reference_read, 0); (I don't like the last line. For lvalue, I think the method name is simply "="; but what is the method name for rvalue?) Yes, this is in priciple the same as creating the accessor function in Ruby, but won't it be useful in general? Or I guess so far nobody needs a general-purpose reference/pointer class in Ruby. Regards, Bill =========================================================================== David Alan Black wrote: > [My understanding, subject to corrective intervention] > The thing is that in Ruby, every variable holds a reference to some > object, and variables are all at the same level of remove from their > objects. (This is probably wrong or different for immutables, but I'm > excluding them for the moment.) So if you have: > a = [1,2,3] > there's no way to get a "reference to a", the way you can have > "pointer to pointer [to pointer [...]]" in C. What you *can* have is > other references to the same object: > b = a > So in Ruby, rather than the infinitely regressing line of pointer > relationships, you get something more like a circle of references, all > equidistant from the object and all terminal (in the sense that > they're at the furthest possible level away from the object). > David > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav