[英]What does it mean to have modules inside other modules in Ruby?
module Fabrication
module Syntax
# Extends Fabrication to provide make/make! class methods, which are
# shortcuts for Fabricate.build/Fabricate.
#
# Usage:
#
# require 'fabrication/syntax/make'
#
# User.make(:name => 'Johnny')
#
#
module Make
def make(*args, &block)
overrides = Fabrication::Support.extract_options!(args)
klass = name.underscore.to_sym
fabricator_name = args.first.is_a?(Symbol) ? "#{klass}_#{args.first}" : klass
Fabricate.build(fabricator_name, overrides, &block)
end
def make!(*args, &block)
overrides = Fabrication::Support.extract_options!(args)
klass = name.underscore.to_sym
fabricator_name = args.first.is_a?(Symbol) ? "#{klass}_#{args.first}" : klass
Fabricate(fabricator_name, overrides, &block)
end
end
end
end
Object.extend Fabrication::Syntax::Make
It is essentially namespacing. 它本质上是命名空间。 You could do the same thing with just
module Fabrication::Syntax::Make
. 您可以只使用
module Fabrication::Syntax::Make
来执行相同的操作。 For whatever reason, nesting them is what most well-known gems/projects do, and for that unknown reason I do too. 无论出于何种原因,嵌套它们都是大多数知名的gem / projects所做的,并且出于未知的原因,我也这样做。 I would love to get some insight as to why people generally prefer nesting over the more direct method.
我很想了解为什么人们通常更喜欢嵌套而不是更直接的方法。
As you in your last line. 正如您在最后一行。 If you define
module
inside of other module
, it will be namespaced. 如果您在其他
module
定义module
,它将被命名空间。 So with your code Make
module will be accessible from outside Fabrication
module definition with its namespace: 因此,通过您的代码,可以从外部
Fabrication
模块定义及其名称空间访问Make
模块。
Fabrication::Syntax::Make
This allows you to define module Make
in root namespace without naming conflict. 这样,您就可以在根名称空间中定义模块
Make
,而无需命名冲突。
Namespacing. 命名空间。 you can drill into nested namespaces with the
::
operator. 您可以使用
::
运算符深入嵌套的名称空间。
Check this out: 看一下这个:
module Galaxy
module StarSystem
module Planet
end
Galaxy # references Galaxy
StarSystem # references Galaxy::StarSystem
Planet # references Galaxy::StarSystem::Planet
end
end
Galaxy # references the Galaxy module
Galaxy::StarSystem::Planet # References the Planet module declared above.
Planet # Exception! No constant Planet exists in this namespace
As you can see, this allows you structure your code in a manner that keeps things modular. 如您所见,这允许您以保持事物模块化的方式来构造代码。 You can write a component that uses man different classes and modules, but are all housed under a single namespace.
您可以编写使用不同类和模块的组件,但所有组件都位于一个命名空间下。 From within that code you can easily access any constant declared in it's own namespace or that of a parent namespace.
从该代码中,您可以轻松访问在其自己的名称空间或父名称空间中声明的任何常量。 But other code cannot see these constants unless you explicitly drill into them.
但是其他代码看不到这些常量,除非您明确钻入它们。
The result is well organized and structured components, which can be easily mixed with other components because they exist entirely within a single name that is unlikely to conflict with other code in the project. 结果是组织良好且结构良好的组件,可以轻松地与其他组件混合,因为它们完全存在于单个名称中,不太可能与项目中的其他代码冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.