是_a? 和 dRuby 对象

发布于 2024-07-07 06:00:25 字数 356 浏览 12 评论 0原文

我正在使用 dRuby,基本上我正在调用一个远程方法来返回一个对象。

在客户端我有这样的代码:

handle_error(response) if response.is_a?(Error)

其中响应是 DRbObject。 (我在使用 dRuby 之前开发了这段代码,如果出现问题,我将返回一个 Error 对象)。 问题是现在

response.is_a?(Error)

返回“false”,因为该对象实际上是 DRbObject。 关于如何检查我的应用程序对象的类有什么想法吗?

谢谢! 罗伯托

I'm working with dRuby and basicly I'm calling a remote method that returns me an object.

In the clientside I have this code:

handle_error(response) if response.is_a?(Error)

where response is the DRbObject. (I've developed this code before using dRuby and I'm returning an Error object if something went wrong). The problem is that now

response.is_a?(Error)

comes back with "false" because the object is actually a DRbObject.
Any idea on how I can check the class of my application object?

Thanks!
Roberto

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

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

发布评论

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

评论(2

倾城泪 2024-07-14 06:00:25

虽然我不确定 DRb 如何管理远程对象,但我希望它能修改 #kind_of? 将类层次结构保留在远程对象上,因此您可以这样做:

response.kind_of?(Error)

如果这不起作用,您可以随时询问它是否响应特定方法并从那里开始

response.respond_to?(some_method_on_your_errors)

Although I'm not sure how DRb manages the remote objects, I'd expect it to modify #kind_of? to keep the class hierarchy on the remote object, so you could do:

response.kind_of?(Error)

If this doesn't work you can always ask it if it responds to an specific method and go from there

response.respond_to?(some_method_on_your_errors)
套路撩心 2024-07-14 06:00:25

您不能使用 Duck 解决该问题吗正在输入? 检查对象是否响应调用以获取错误信息,而不是检查对象是否为错误。 如果是,则根据该信息处理错误,否则处理(非错误)响应。

Could you not work around the problem by using Duck Typing? Instead of checking for whether the object is an Error, check whether the object responds to a call to get the error information. If it does, handle the error according to that information, otherwise handle the (non-error) response.

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