Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use pointers in Ruby?

Tags:

I think Ruby is interpreted to C. If so, how can I use the concept of pointers and other features that are parts of C environment? How can we utilize the power of C with the simplicity of Ruby?

like image 797
user914312 Avatar asked Aug 26 '11 18:08

user914312


People also ask

Does Ruby use pointers?

In `ruby`, the body of an object is expressed by a struct and always handled via a pointer. A different struct type is used for each class, but the pointer type will always be `VALUE` (figure 1). In practice, when using a `VALUE`, we cast it to the pointer to each object struct.


1 Answers

In Ruby, (almost) every variable is in fact a reference/pointer to an object, e.g.

a = [0, 1, 23] b = a a << 42 p b 

will give [0, 1, 23, 42] because a and b are pointing to the same object.

So in fact, you are using pointers all the time.

If you want to do pointer arithmetic as in C, this is not possible with Ruby.

like image 127
undur_gongor Avatar answered Oct 19 '22 08:10

undur_gongor