类中的 Ruby 类(或模块中的模块) [英] Ruby Classes within Classes (or Modules within Modules)

查看:33
本文介绍了类中的 Ruby 类(或模块中的模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

class Outerclass
  def foobar
    puts "FOOBAR"
  end

  class Innerclass   
    def barfoo
      puts "BARFOO"
    end
  end
end

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

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

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

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

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

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

这是我的问题:

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

  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.

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

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

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

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

Outerclass 的实例是否知道 Innerclass 类的任何信息?

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

Innerclass 的实例是否知道 Outerclass 类的任何信息?

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

感谢您提供的任何帮助!

Appreciate any help you can provide!

推荐答案

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

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

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

So when Ruby encounters the nested class, it assigns it to constant InnerClass which is assigned to constant OuterClass They have no relation to each other whatsoever.

InnerClass 没有继承到 OuterClass:

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

当您调用 OuterClass::InnerClass 常量时,您指的是 InnerClass 常量,该常量位于 OuterClass 常量下,该常量等于 Class.new 分配给它的语句.

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

一本关于这方面的好书是Metaprogramming Ruby".它详细介绍了类、单例、模块等.

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

这篇关于类中的 Ruby 类(或模块中的模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆