How to Emulate C/C++ Pointer/Reference in Ruby?

Hi,

Is there any easy way to emulate C pointer or C++ reference in Ruby?

Basically, the problem is something like this. I have a root object,
and it contains an instance variable that refers to, say, a file.
When the root object is created, it opens the file. Then the root
object creates many other child objects, passing the value of the file
variable in the process. Any child object will write to the same
file.

Now, in the middle of execution, the root object may close the file,
and some other program may access the file. When the root object
reopens the file, all the file variables in the child objects are no
longer valid. Therefore, I think it will be nice if the we can have
some “reference” or “pointer”, so that the child objects’ file
variables always point to the root object file variable.

The only solution that I can think of for now is for the root object
to remember all the child objects, and to modify all the child
objects’ file variables whenever the root object file variable
changes. However, to me this is more like design pattern than a
language feature. (I read somewhere that in general the issue with
programming languages is not whether something is possible, but rather
whether the thing is facilitated.)

Regards,

Bill

The only solution that I can think of for now is for the root object
to remember all the child objects, and to modify all the child
objects' file variables whenever the root object file variable
changes.

Well, rather than give the File object to the Childs just give it the Root
object. Then the Child objects make reference to the File object via a
Root method

Guy Decoux

You could wrap the File object owned by the Root in another object that
has the same interface as File, but passes methods to the actual File.

When the Root closes and reopens the file, it can change the File
reference in the wrapper. The Child objects can keep reading from the
wrapper objects they have been given, oblivious to the fact that they
are now actually reading data from a different, hidden File object.

Cheers,
Nat.

···

On Wed, 2002-07-31 at 14:58, Bill Tj wrote:

Hi,

Is there any easy way to emulate C pointer or C++ reference in Ruby?

Basically, the problem is something like this. I have a root object,
and it contains an instance variable that refers to, say, a file.
When the root object is created, it opens the file. Then the root
object creates many other child objects, passing the value of the file
variable in the process. Any child object will write to the same
file.

Now, in the middle of execution, the root object may close the file,
and some other program may access the file. When the root object
reopens the file, all the file variables in the child objects are no
longer valid. Therefore, I think it will be nice if the we can have
some “reference” or “pointer”, so that the child objects’ file
variables always point to the root object file variable.

The only solution that I can think of for now is for the root object
to remember all the child objects, and to modify all the child
objects’ file variables whenever the root object file variable
changes. However, to me this is more like design pattern than a
language feature. (I read somewhere that in general the issue with
programming languages is not whether something is possible, but rather
whether the thing is facilitated.)

Regards,

Bill

Dr. Nathaniel Pryce, Technical Director, B13media Ltd.
Studio 3a, 22-24 Highbury Grove, London N5 2EA, UK
http://www.b13media.com

Hi,

Is there any easy way to emulate C pointer or C++ reference in Ruby?

There is no need to emulate references. With the exception of certain
primitive types (Fixnum, TrueClass, FalseClass, NilClass), all objects
are passed by reference in Ruby.

Basically, the problem is something like this. I have a root object,
and it contains an instance variable that refers to, say, a file.
When the root object is created, it opens the file. Then the root
object creates many other child objects, passing the value of the file
variable in the process. Any child object will write to the same
file.

Now, in the middle of execution, the root object may close the file,
and some other program may access the file. When the root object
reopens the file, all the file variables in the child objects are no
longer valid. Therefore, I think it will be nice if the we can have
some “reference” or “pointer”, so that the child objects’ file
variables always point to the root object file variable.

Are you reopening your file with:
@file.close
@file = File.open(“newfile”)

or with:
@file.reopen(“newfile”)

the former creates a new object; the latter re-uses the original object.

The only solution that I can think of for now is for the root object
to remember all the child objects, and to modify all the child
objects’ file variables whenever the root object file variable
changes. However, to me this is more like design pattern than a
language feature. (I read somewhere that in general the issue with
programming languages is not whether something is possible, but rather
whether the thing is facilitated.)

Some other options:

  1. Use a delegator. A SimpleDelegator can change the object it is
    delegating to on-the-fly. The children get a reference to the
    delegator instead of to the IO object you are using.
  2. Use the observer pattern. Whenever you change which file you are
    accessing, mark your state as changed and notify your observers
    that they need to refetch their references.
  3. Use ts’s suggestion of always accessing the file object via an
    accessor in the root object.

Paul

···

On Wed, Jul 31, 2002 at 10:58:50PM +0900, Bill Tj wrote:

Thanks for all the responses. For efficiency purposes (because the file
is accessed so often), probably I prefer 2) rather than 3). Depending on
the implementation, I guess, 1) will be somewhere between 2) and 3).

However, this is only for the one specific example. I guess, then, indeed
there is no way to emulate pointer in Ruby. I used Perl not for long
before I switched to Ruby. If I am not mistaken, in Perl there is stuff
called reference.

Regards,

Bill

···

========================================================================

Some other options:

  1. Use a delegator. A SimpleDelegator can change the object it is
    delegating to on-the-fly. The children get a reference to the
    delegator instead of to the IO object you are using.
  2. Use the observer pattern. Whenever you change which file you are
    accessing, mark your state as changed and notify your observers
    that they need to refetch their references.
  3. Use ts’s suggestion of always accessing the file object via an
    accessor in the root object.

Paul

Hello –

Thanks for all the responses. For efficiency purposes (because the file
is accessed so often), probably I prefer 2) rather than 3). Depending on
the implementation, I guess, 1) will be somewhere between 2) and 3).

However, this is only for the one specific example. I guess, then, indeed
there is no way to emulate pointer in Ruby. I used Perl not for long
before I switched to Ruby. If I am not mistaken, in Perl there is stuff
called reference.

[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

···

On Thu, 1 Aug 2002, William Djaja Tjokroaminata wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

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:

nil, true, false and FixNum are internally stored by value. The runtime
checks for these special types and so if you type:

32.class

it gives you what you’d expect: “FixNum”. Even though:

val = 32

Stores the value in ‘val’ rather than creating a FixNum object of value 32
and setting ‘val’ as a reference to it. That would have been easier and
more consistent but would have increased object creation and memory
footprint. The price you pay is loosing 1 bit of information for FixNums.
:slight_smile:

Internally, I’m not sure why nil, true and false aren’t handled as
references to instanced objects of class NilClass, TrueClass and FalseClass.
This would reduce the number of internal special case checks…Matz?

···


Justin Johnson.

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 dblack@candle.superlink.net 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

This is exactly how references work in C++, as well, e.g.:

#include

void foo(int & a) {
std::cout << &a << std::endl; // print the address of a
}

void bar(int & b) {
std::cout << &b << std::endl; // print the address of b
foo(b); // foo gets a reference, not a reference to a reference
}

int main()
{
int x = 42;
bar(x);
}

Should print something like:

0xbffff184
0xbffff184

because a and b are both references to the same object.

Pointers, however, are a different story, altogether.

(/me ignores the “Pointers, however, are a different story” echoes from
the “Airplane” fans).

Paul

···

On Thu, Aug 01, 2002 at 03:03:47AM +0900, David Alan Black wrote:

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