ruby救援不断失踪?
几个月前,我一直在使用一些 ruby 库(不幸的是,我不记得到底是哪一个),
我很惊讶地发现它允许我用类似的东西初始化它的实例:
Lib::SOMETHING(args)
我真的不明白它是如何实现的可能会起作用。我非常确定它应该是动态的(没有任何常量),例如constant_missing模块方法,或者可能以某种方式处理ConstantMissing异常。
您能给建议吗?
few months ago I've been using some ruby library ( I can't recall which one exactly, unfortunately )
I've been surprised to see it allowed me to initialize it's instance with something like that:
Lib::SOMETHING(args)
I don't really understand how it could possibly work. I'm pretty much sure it should be something dynamic ( there's no SOMETHING constant ) like constant_missing
module method or maybe the ConstantMissing
exception gets handled somehow.
Could you please advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上没有什么神奇之处,它只是看起来不熟悉:
Net::HTTP
与其Proxy
类做了类似的事情,它允许诸如Net::HTTP 之类的调用: :Proxy(...)
,它的定义类似:要点是,对于类方法,您可以互换使用
::
或.
来调用它们。No magic there in theory, it just looks unfamiliar:
Net::HTTP
does something similar with itsProxy
class, it allows calls such asNet::HTTP::Proxy(...)
, it is defined similarly:The punch line is that for class methods you can interchangeably use
::
or.
to invoke them.