module Superpower
# instance method
def turn_invisible
...
end
# module method
def Superpower.turn_into_toad
...
end
module Fly
def flap_wings
...
end
end
end
Class Superman
include Superpower
...
def run_away
# how to call flap_wings?
# how to call turn_invisible?
end
def see_bad_guys(bad_guy = lex_luthor)
#is this correct?
Superpower.turn_into_toad(bad_guy)
end
end
Hi I saw some ruby code which I couldn't understand. How do you call flap_wings from within the Superman class? Is it possible to call an instance method from within the class? What is the difference between including modules and embedding modules? Why and when should you do that?
嗨,我看到了一些我无法理解的红宝石代码。你如何从超人课程中调用flap_wings?是否可以从类中调用实例方法?包含模块和嵌入模块有什么区别?为什么以及何时应该这样做?
2 个解决方案
#1
I'm assuming that when you say embedding a module you mean the "Fly" module from your example is embedded in "Superpower".
我假设当你说嵌入模块时你的意思是你的例子中的“Fly”模块嵌入在“Superpower”中。
If that is the case, I would call it a nested module. The only time I would use a Nested Module is when the nested module deals specifically with the main module, such that the code in Fly is directly correlated to Superpower, but are separated for convenience and readability.
如果是这种情况,我会称之为嵌套模块。我唯一一次使用嵌套模块是嵌套模块专门处理主模块时,Fly中的代码与Superpower直接相关,但为了方便和可读性而分开。
You can use the nested module's methods simply by including superpower first, then fly second, like so:
您可以使用嵌套模块的方法,只需先包含超级大国,然后再飞第二次,如下所示:
Class Superman
include Superpower
include Fly
# ...
end
The details are described further on this blog.
详细信息将在此博客中进一步说明。
#2
You want to read the documentation on mixins which are a way to workaround the fact that Ruby has only single inheritance. By including a given module A in a class B, all the modules methods in A are available as if they were actually part of class B.
您想阅读有关mixins的文档,这是一种解决Ruby只有单一继承这一事实的方法。通过在B类中包含给定的模块A,A中的所有模块方法都可用,就像它们实际上是B类的一部分一样。
That means that calling turn_invisible
is as easy as
这意味着调用turn_invisible非常简单
def run_away
turn_invisible
end
For flap_wings
as it is in another namespace, it may be as easy as:
对于另一个命名空间中的flap_wings,它可能很简单:
def fly_away
Fly.flap_wings
end
but I haven't not tried to complete your code and "run" it.
但我并没有尝试完成你的代码并“运行”它。
Mixins are explained here and there.
Mixins在这里和那里解释。
#1
I'm assuming that when you say embedding a module you mean the "Fly" module from your example is embedded in "Superpower".
我假设当你说嵌入模块时你的意思是你的例子中的“Fly”模块嵌入在“Superpower”中。
If that is the case, I would call it a nested module. The only time I would use a Nested Module is when the nested module deals specifically with the main module, such that the code in Fly is directly correlated to Superpower, but are separated for convenience and readability.
如果是这种情况,我会称之为嵌套模块。我唯一一次使用嵌套模块是嵌套模块专门处理主模块时,Fly中的代码与Superpower直接相关,但为了方便和可读性而分开。
You can use the nested module's methods simply by including superpower first, then fly second, like so:
您可以使用嵌套模块的方法,只需先包含超级大国,然后再飞第二次,如下所示:
Class Superman
include Superpower
include Fly
# ...
end
The details are described further on this blog.
详细信息将在此博客中进一步说明。
#2
You want to read the documentation on mixins which are a way to workaround the fact that Ruby has only single inheritance. By including a given module A in a class B, all the modules methods in A are available as if they were actually part of class B.
您想阅读有关mixins的文档,这是一种解决Ruby只有单一继承这一事实的方法。通过在B类中包含给定的模块A,A中的所有模块方法都可用,就像它们实际上是B类的一部分一样。
That means that calling turn_invisible
is as easy as
这意味着调用turn_invisible非常简单
def run_away
turn_invisible
end
For flap_wings
as it is in another namespace, it may be as easy as:
对于另一个命名空间中的flap_wings,它可能很简单:
def fly_away
Fly.flap_wings
end
but I haven't not tried to complete your code and "run" it.
但我并没有尝试完成你的代码并“运行”它。
Mixins are explained here and there.
Mixins在这里和那里解释。