6

考虑 Ruby 类Foo::Bar

约定是让 'Foo' 命名空间成为一个模块,但它也可以很容易地成为一个类:

module Foo; class Bar; end; end

相对:

class Foo; class Bar; end; end

在第二种情况下,Bar不是 的内部类Foo,它只是在 的单例上定义的另一个常量Foo。在这两种情况下,超类都是 Object 并且它们只包含 Kernel 模块。他们的祖先链是相同的。

因此,除了您可以Foo根据其类进行的操作(如果是类则实例化,如果是模块则扩展/包含),命名空间的性质是否对Bar? 是否有令人信服的理由选择一个多个名称间距而不是另一个?

我看到你能做的唯一奇怪的事情是Foo::Bar.new.extend FooFoo.new.class::Bar

我自己对在类中定义的类最常见的用法是帮助器结构/类,它只能在类内部使用:

class Foo
  Bar = Struct.new(:something) do
    def digest
      puts "eating #{something}"
    end
  end
  def eat(something)
    Bar.new(something).digest
  end
end

我发现最接近这个讨论的是“使用类与模块在 Ruby 中打包代码”。

4

1 回答 1

5

使用模块进行命名空间最直接的好处是您可以使用它include来导入命名空间,并使用其中声明的非限定常量:

module Foo; class Bar; end; end

module Elsewhere
  include Foo
  Bar.new
end
于 2013-10-24T19:07:40.543 回答