方法中包含 Ruby 模块
在类 Foo
中,我想在某些条件下包含方法 Bar
:
module Bar
def some_method
"orly"
end
end
class Foo
def initialize(some_condition)
if !some_condition
"bar"
else
class << self; include Bar; end
end
end
end
是否有任何更清晰(和更清晰)的方法来实现 include
该方法无需在单例类中执行?
In class Foo
I'd like to include method Bar
under certain conditions:
module Bar
def some_method
"orly"
end
end
class Foo
def initialize(some_condition)
if !some_condition
"bar"
else
class << self; include Bar; end
end
end
end
Is there any cleaner (and clearer) way to achieve the include
in the method without having to do it inside the singleton class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
extend
相当于单例类中的include
:extend
is the equivalent ofinclude
in a singleton class: