在Ruby中,如何在外部类中定义内部类访问变量/方法?

发布于 2025-02-09 16:48:39 字数 1226 浏览 2 评论 0原文

示例:

require "fluent/plugin/output"

module Fluent::Plugin
    class Outer < Fluent::Plugin::Output
        @outer_var = "Hello from outer variable"

        class Inner
            def initialize()
                puts "@outer_var", @outer_var # nil
                log.info "Hello from inner initialize" # undefined local variable or method log
                outer_method() # undefined method `outer_method' for #<Outer::Inner:0x00007fd2100452b0> (NoMethodError)
            end
        end # Inner

        def initialize()
            Inner.new()
            log.info "Hello from outer initialize" # works
        end

        def outer_method
            puts "Hello from outer method"
        end

    end # Outer
end # Fluent::Plugin

如何使内部类可访问外部变量和方法?示例在这里。我了解内部/外部类的概念在Ruby中不存在,根据 this 答案。最佳方法是什么使用内部外部类,而仍然是output模块的一部分。

Example:

require "fluent/plugin/output"

module Fluent::Plugin
    class Outer < Fluent::Plugin::Output
        @outer_var = "Hello from outer variable"

        class Inner
            def initialize()
                puts "@outer_var", @outer_var # nil
                log.info "Hello from inner initialize" # undefined local variable or method log
                outer_method() # undefined method `outer_method' for #<Outer::Inner:0x00007fd2100452b0> (NoMethodError)
            end
        end # Inner

        def initialize()
            Inner.new()
            log.info "Hello from outer initialize" # works
        end

        def outer_method
            puts "Hello from outer method"
        end

    end # Outer
end # Fluent::Plugin

How can I make the outer variables and methods accessible to the inner class? Example here shows how inner/outer classes are defined but not how outer class may be accessed. I understand that the concept of inner/outer classes don't exists in Ruby, as per this answer. What is the best way use the Inner and Outer class while still being part of the output module.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

城歌 2025-02-16 16:48:40

在Ruby中,内部类访问变量/方法如何?

Ruby没有“内部”或“外部”类的概念。听起来您来自Beta,Scala或Newspeak之类的语言,但Ruby不是Beta,Scala或Newspeak。一般而言,任何编程语言都可以正常工作,而编程语言的规范是如何有效的,而不是某些其他编程语言的规范如何工作。 Beta,Scala和Newspeak的课程嵌套了,但Ruby没有。

在Ruby中,您可以词法嵌套 a class 定义,但这不会创建嵌套类。如果将类的定义嵌套在另一个类的定义中,则不会在这两个类之间创建任何类型的关系。没有任何。您不能从另一个类中“访问一个类别的变量/方法”,因为您可以遵循的两个类之间没有关系以获取这些变量/方法。

嵌套类定义唯一的事情是命名空间由外部类的内类定义定义的常数。就是这样。这两个类之间没有关系,唯一的关系是 constant 和类之间。

只是不可能。

在Ruby中创建关系的方法是 shastaritance ,而不是嵌套(因为没有嵌套)。在Ruby中创建对象之间的关系的方法是关联,聚合或组成,而不是嵌套。

Ruby IS 足够灵活,可以实现像“适当” beta式内部类别的行为。请记住,一个内类是在外部类的对象实例中“嵌套”的。我们可以使用一个实例变量模拟这一点:

class Outer
  attr_reader :Inner

  def outer_method
    __callee__
  end

  private

  attr_writer :Inner

  def initialize
    self.Inner = Class.new(self.class) do
      def inner_method
        [__callee__, outer_method]
      end
    end
  end
end

outer = Outer.new
inner = outer.Inner.new
p inner.inner_method

请注意,我使内部类从外部类中继承:那是在Ruby中进行行为共享的方法。您需要具有某种形式的继承或关联关系。嵌套不会产生关系。

In Ruby, how can inner class access variables/methods defined in outer class?

Ruby does not have a concept of "inner" or "outer" classes. It sounds like you are coming from a language like BETA, Scala, or Newspeak, but Ruby is not BETA, Scala, or Newspeak. As a general rule, any programming language works exactly how the specification for the programming language says it works, not how the specification for some other programming language says it works. BETA, Scala, and Newspeak have nested classes, but Ruby has not.

In Ruby, you can lexically nest a class definition, but that does not create a nested class. If you nest the definition of a class inside the definition of another class, that does not create any sort of relationship whatsoever between those two classes. None. You cannot "access variables/methods" of one class from the other class because there is no relationship between those two classes you could follow in order to get at those variables/methods.

The only thing a nested class definition does, is namespace the constant defined by the inner class definition to the outer class. That's it. There is no relationship between the two classes, the only relationship is between the constant and the class.

It is just not possible.

The way to create relationships between classes in Ruby is inheritance, not nesting (because there is no nesting). The way to create relationships between objects in Ruby is association, aggregation, or composition, not nesting.

Ruby is flexible enough to implement something that behaves like a "proper" BETA-style inner class. Remember, an inner class is "nested" inside an object instance of the outer class. We can emulate that using an instance variable:

class Outer
  attr_reader :Inner

  def outer_method
    __callee__
  end

  private

  attr_writer :Inner

  def initialize
    self.Inner = Class.new(self.class) do
      def inner_method
        [__callee__, outer_method]
      end
    end
  end
end

outer = Outer.new
inner = outer.Inner.new
p inner.inner_method

Please note that I made the inner class inherit from the outer class: that is the way to do behavior sharing in Ruby. You need to have some form of inheritance or association relationship. The nesting does not create a relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文