Accessing objects memory address in ruby..?

ruby

Solution

Yes.

From "Fiddling with Ruby’s Fiddle":

"You can get the actual pointer value of an object by taking the object id, and doing a bitwise shift to the left. This will give you the pointer (or memory location) of the ruby object in memory."

Using your example of `i = 5` it could be done like so:

i = 5
i_ptr_int = i.object_id << 1
=> 22

"In Ruby, why does inspect() print out some kind of object id which is different from what object_id() gives?" has more info about `object_id`, including a brief introduction to the C source underlying the implementation which you might find helpful.

Take a look at "Fiddle" for some other cool things you can do.

Problem

Is there any way in Ruby to get the memory address of objects? ``` (i = 5) ``` Is it possible to get the memory address of that object `5`? I have been trying to get this over some time.

Original source

Related problems