在 Ruby 中,如何为从“ENV”等常量获取的对象设置别名?
鉴于 ENV
既不是类也不是模块,因此不可能使用以下常用机制直接为其单例方法之一添加别名:
class SomeClass
class << self
alias some_alias some_existing_method
end
end
是否有一种方法可以为类似 ENV 的方法添加别名::[]?
Given that ENV
is neither a class nor a module, it is not possible to directly alias one of its singleton methods with the following usual mechanism:
class SomeClass
class << self
alias some_alias some_existing_method
end
end
Is there nonetheless a way to alias a method like ENV::[]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的,技巧是在单例类的上下文中评估别名语句:
Yes, it's possible, the trick is to evaluate the alias statement within the context of the singleton class: