Ruby - 如何重现指针/引用行为?

发布于 2024-09-11 11:09:16 字数 165 浏览 2 评论 0原文

我目前正在尝试为新应用程序创建一个引擎。

设计非常复杂,我确实需要一种方法来创建引用或指针或任何远程修改和访问数据的方法。数据必须保存在一个地方,并且不得重复。

有没有办法通过引用获取对象? 例如,我可以获取带有 object_id 的类的实例吗?

感谢您的帮助 ;)

I am currently trying to create an engine for a new application.

Design is quite complex and i realy need a way to create a reference or a pointer or what ever way to remotely modify and access the datas. Datas must be kept in a single place and must not be duplicated.

Is there a way do get an object by reference ?
For example can I get the instance of a class with his object_id ?

Thanks for your help ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-09-18 11:09:17

以下内容应演示如何获取对对象的引用并取消引用它:

   s = "I am a string"                    #=> "I am a string"
   r = ObjectSpace._id2ref(s.object_id)   #=> "I am a string"
   r == s                                 #=> true

您可能希望查看 对象ObjectSpace

The following should demonstrate how to get a reference to an object and de-reference it:

   s = "I am a string"                    #=> "I am a string"
   r = ObjectSpace._id2ref(s.object_id)   #=> "I am a string"
   r == s                                 #=> true

You may wish to review the documentation for Object and ObjectSpace.

天暗了我发光 2024-09-18 11:09:17

不确定您到底想要实现什么目标。 Ruby 已经使用所有自定义对象的引用。

您可能想查看delegate 库。

require 'delegate'

obj = "foo"
ptr = SimpleDelegator.new(obj)
ptr.upcase # => "FOO"
other_obj = "bar"
ptr.__setobj__(other_obj)  # make ptr point to another object 

Not sure exactly what you want to achieve. Ruby uses references for all custom objects already.

You may want to look at the delegate library.

require 'delegate'

obj = "foo"
ptr = SimpleDelegator.new(obj)
ptr.upcase # => "FOO"
other_obj = "bar"
ptr.__setobj__(other_obj)  # make ptr point to another object 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文