类中的 Ruby 类(或模块中的模块)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14739640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:44:08  来源:igfitidea点击:

Ruby Classes within Classes (or Modules within Modules)

ruby

提问by wmock

As I'm reading different Ruby books, I've noticed that Ruby classes can be defined within other Ruby classes or modules. Here's an example of a class within a class:

当我阅读不同的 Ruby 书籍时,我注意到可以在其他 Ruby 类或模块中定义 Ruby 类。这是一个类中的类的示例:

class Outerclass
  def foobar
    puts "FOOBAR"
  end

  class Innerclass   
    def barfoo
      puts "BARFOO"
    end
  end
end

Here's some code that I ran in IRB to try to understand this conceptually:

这是我在 IRB 中运行的一些代码,试图从概念上理解这一点:

oc = Outerclass.new # => #<Outerclass:0x00000100a46478>

Outerclass.instance_methods(false) # => [:foobar]

ic = Outerclass::Innerclass.new # => #<Outerclass::Innerclass:0x00000100a0b120>

ic = Outerclass::Innerclass.instance_methods(false) # => [:barfoo]

Here are my questions:

以下是我的问题:

  1. When the ruby interpreter first encounters my Class definition code above, does it go through the methods I've written and store it somewhere? I know that the instance method "foobar" doesn't actually get run since there's no call being made to it within the Outerclass definition.

  2. Piggybacking off the 1st question, what about when Ruby encounters the class Innerclass? What happens here?

  3. In general, what are some reasons why you would want to have classes within classes? Are there any advantages to doing this?

  4. Does an instance of Outerclass know anything about the class Innerclass?

  5. Does an instance of Innerclass know anything about the class Outerclass?

  1. 当 ruby​​ 解释器第一次遇到我上面的类定义代码时,它是否会通过我编写的方法并将其存储在某处?我知道实例方法“foobar”实际上并没有运行,因为在 Outerclass 定义中没有调用它。

  2. 捎带第一个问题,当 Ruby 遇到类 Innerclass 时怎么办?这里会发生什么?

  3. 一般来说,您希望在类中设置类的原因有哪些?这样做有什么好处吗?

  4. Outerclass 的实例是否知道有关类 Innerclass 的任何信息?

  5. Innerclass 的实例是否了解 Outerclass 类的任何信息?

Appreciate any help you can provide!

感谢您提供的任何帮助!

回答by Michael Papile

When the interpreter is going through this file, it is assigning classes like this:

当解释器通过这个文件时,它会分配这样的类:

OuterClass = Class.new do 
  def foobar
    puts "FOOBAR"
  end

  InnerClass = Class.new do   
    def barfoo
      puts "BARFOO"
    end
  end
end

So when Ruby encounters the nested class, it assigns it to constant InnerClasswhich is assigned to constant OuterClassThey have no relation to each other whatsoever.

因此,当 Ruby 遇到嵌套类时,它会将其InnerClass分配给常量,而常量又分配给了常量。OuterClass它们彼此之间没有任何关系。

The InnerClasshas no inheritance to OuterClass:

InnerClass没有继承OuterClass

  InnerClass.ancestors
  => [InnerClass, Object, Kernel, BasicObject]

When you call OuterClass::InnerClassconstant you are refering to the InnerClassconstant that is namespaced under the OuterClasscontstant which equals the Class.new statement assigned to it.

当您调用OuterClass::InnerClass常量时,您InnerClass指的是在OuterClass常量下命名空间的常量,该常量等于分配给它的 Class.new 语句。

A good book to read about this is "Metaprogramming Ruby". It goes into the details of classes, singletons, modules etc.

关于这方面的一本好书是“Metaprogramming Ruby”。它进入了类、单例、模块等的细节。