从套接字连接导入 Ruby 类?

发布于 2024-12-01 02:57:50 字数 489 浏览 1 评论 0原文

我对客户端/服务器原型有一个想法,其中服务器将保存 Marshal.dump 类对象的哈希值及其版本号。然后,客户端可以查询服务器有关版本号的信息,并在实例化该类之前导入该类的较新版本:

class Stuff
  def methods
    gibberish
  end
end

$obj_hash["Stuff"] = [3.0, Marshal.dump(Stuff)]

我遇到的问题是,Ruby 似乎不允许我 Marshal.load 数据。已从服务器下载它,因为客户端中不存在该类及其方法。如果我通过创建“虚拟”类来绕过此问题,则无法用 Marshal.load'ed 数据替换虚拟类。如果我只是尝试将加载的数据用作类,它会根据虚拟类的内容而不是下载的内容来运行。

还有其他方法可以解决这个问题吗?如果没有,那么我想我可以只 gz 代码,然后在另一端对其进行评估,但我试图避免使用 eval 或通过线路发送易于解读的代码。

预先感谢您的任何建议。

I have this idea for a client/server archetype where the server would hold a hash of Marshal.dump'ed class objects along with their version numbers. Then the client could query the server concerning the version number and import the newer version of the class before instantiating it:

class Stuff
  def methods
    gibberish
  end
end

$obj_hash["Stuff"] = [3.0, Marshal.dump(Stuff)]

The problem I'm running into is that Ruby doesn't seem to want to allow me to Marshal.load the data once I've downloaded it from the server because the class and its methods don't exist in the client. If I bypass this by creating a 'dummy' class I'm then unable to replace the dummy class with the Marshal.load'ed data. If I simply try to use the loaded data as a class it functions according to the contents of the dummy class rather than the downloaded one.

Is there another way to go about this? If not then I guess I could just gz the code and then eval it at the other end, but I'm trying to avoid using eval or sending easily decipherable code over the line.

Thanks in advance for any advice.

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

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

发布评论

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

评论(1

揪着可爱 2024-12-08 02:57:50

看看会发生什么。

class Stuff
  def methods
    "foo"
  end
end

ruby-1.8.7-p352 :001 > Marshal.dump(Stuff) 
 => "\004\bc\bStuff"

请注意它没有提到“methods”或“foo”。如果服务器没有发送该代码,那么客户端如何知道 Stuff#methods 应该做什么?

不会的。 :)

要执行您想做的操作,您必须发送代码本身并对其进行评估。当然,您必须自己实现版本控制逻辑,并“真正重新定义”它们的类(而不仅仅是猴子补丁)。

请参阅你允许在 ruby​​ 中重新定义一个类吗?或者这只是在 irb 中

Look at what happens.

class Stuff
  def methods
    "foo"
  end
end

ruby-1.8.7-p352 :001 > Marshal.dump(Stuff) 
 => "\004\bc\bStuff"

Notice how it says nothing about "methods" or "foo." If the server isn't sending that code down the wire, how is the client supposed to know what Stuff#methods should do?

It won't. :)

To do what you want to do, you'll have to send down the code itself and eval it. You'll have to implement the versioning logic yourself, of course, and "really re-define" the classes (not just monkey-patch) them.

See are you allowed to redefine a class in ruby? or is this just in irb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文