Ruby 向库代码添加模块的首选方法?
将模块功能添加到库 ruby 代码的首选方法是什么?
考虑以下问题:
module MyExceptions
class SomethingBadHappenedTheLibarayDesignerDidntConsider < StandardError; end
end
如何将 MyExceptions
模块添加到我无法控制的类中?
更新,在 my_library_class_exceptions.rb
中我做了:
class LibraryClass
include MyExceptions
end
module MyExceptions
class SomethingBadHappenedTheLibarayDesignerDidntConsider < StandardError; end
end
但是控制台返回:NameError:未初始化的常量 LibraryClass::MyExceptions
What's the preferred method to add module functionality to library ruby code?
Consider the following:
module MyExceptions
class SomethingBadHappenedTheLibarayDesignerDidntConsider < StandardError; end
end
How can I add MyExceptions
module to a class that I don't control?
Update, in my_library_class_exceptions.rb
I did:
class LibraryClass
include MyExceptions
end
module MyExceptions
class SomethingBadHappenedTheLibarayDesignerDidntConsider < StandardError; end
end
But the console returns: NameError: uninitialized constant LibraryClass::MyExceptions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Ruby 中,您可以稍后打开任何类的定义并更改它。
下面是一个人为的示例,用于通过重新打开以前创建的类来覆盖除法以使用自定义异常。
In Ruby, you can open the definition of any class later and change it.
Below is a contrived example for overriding division to use a custom exception by reopening a previously created class.
查看您的更新,它不起作用的简单原因是顺序:您在定义之前使用 MyExceptions。
文件的固定版本 - 重新打开库类:
这个类 eval 技巧可以说是比简单地再次打开类更安全的方法:如果您错过了类名,它会警告您。
顺便说一句:我真的不明白你是如何计划使用 MyException 那么......这可能意味着更多的猴子修补和/或包装库的部分。
更新:上面假设您在同一目录中有
thelib.rb
,其中至少包含以下内容:Looking at your update, the simple reason it doesn't work is the order: you use MyExceptions before it's defined.
The fixed version of your file - reopening the library class:
This class eval trick is arguably a safer way for reopening class than simply
class LibraryClass
again: it will warn you if you missed the classname.BTW: I don't really see how you're planning the use MyException then.. that would probably mean some more monkey patching and/or wrapping parts of the library.
UPDATE: the above assumes you have
thelib.rb
in the same directory with at least the following content:我通过修复一些东西并改变一些东西来实现这个工作。
事实证明,
LibraryClass
实际上并不是一个类,而是一个模块。所以我改变了:类库类
包括我的异常
事实
into to:
library_class.rb
移动到lib\library_class_extensions.rb
中,并确保lib
是自动加载路径的一部分。require 'library_class_extensions'
我认为这对于我试图完成的行为来说是一个更好的模式,因为这实际上更多的是
app/model 的库类型代码
输入代码。我对每个人给出的答案都投了赞成票,感谢他们的英勇努力。
I got this working by fixing a few things, and changing a few things.
It turns out
LibraryClass
wasn't actually a class, but a module. So I changed:class LibraryClass
include MyExceptions
end
into to:
library_class.rb
intolib\library_class_extensions.rb
and made sure thatlib
was part of the autoload path.require 'library_class_extensions'
I think that's a better pattern for the behavior I was trying to accomplish anyway, since this really is more of library type code that
app/model
type code.I'm up-voting the answers given by everyone, for their heroic efforts.