访问ruby中的对象内存地址..? [英] Accessing objects memory address in ruby..?
问题描述
Ruby 有没有办法获取对象的内存地址?
(i = 5)
是否可以得到那个对象5
的内存地址?
我一直在努力解决这个问题.
是.
来自摆弄 Ruby 的小提琴":><块引用>
您可以通过获取对象 id 并按位向左移动来获取对象的实际指针值.这将为您提供内存中 ruby 对象的指针(或内存位置).">
使用您的 i = 5
示例,可以这样做:
i = 5i_ptr_int = i.object_id <<1=>22
"在 Ruby 中,为什么inspect() 打印出的对象ID 与object_id() 给出的不同?" 有更多关于object_id
的信息,包括对实现底层的 C 源代码的简要介绍,您可能会觉得有帮助.
看看Fiddle" 其他一些你可以做的很酷的事情.
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.
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.
这篇关于访问ruby中的对象内存地址..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!