How can a nested class access a method in the outer class in Ruby?

后端 未结 7 2042
-上瘾入骨i
-上瘾入骨i 2021-02-12 13:58
def class A
  def a
    raise \"hi\" #can\'t be reached
  end

  class B
    def b
      a() #doesn\'t find method a.
    end
  end
end

I want to invok

相关标签:
7条回答
  • 2021-02-12 14:09

    I typically do something like this:

    class A
      def a
        puts "hi"
      end
    
      def createB
        B.new self
      end
    
      class B
        def initialize(parent)
          @parent=parent
        end
    
        def b
          @parent.a
        end
      end
    end
    
    A.new.createB.b
    
    0 讨论(0)
  • 2021-02-12 14:10

    Was a supposed to be a class method for class A?

    class A
      def self.a
        raise "hi"
      end
      class B
        def b
          A::a 
        end
      end
    end
    
    A::B.new.b
    

    If you want to keep it as an instance method, you'll obviously have call to it on an instance, like for example A.new.a.

    0 讨论(0)
  • 2021-02-12 14:11

    Ruby doesn't have nested classes.

    The only way to inherit behavior is, well, via inheritance.

    If you want your code to work, you need to use a language which supports nested classes. While this is an incredibly neat and powerful feature, I unfortunately know of only two languages that have nested classes:

    • BETA, the language which introduced nested classes (and its successor gbeta)
    • Newspeak

    I don't know of any other.

    Java has a construct called nested classes, but they have some unfortunate design limitations.

    In your example above, it's not the class B that is nested inside A, it is the constant B that is nested inside A. Think about this:

    C = A::B
    

    Now, the class is available under two names: A::B and C. It should be immediately obvious that C is global and not nested inside A. (Well, actually, C is nested inside Object, because there aren't really global constants either, but that's beside the point.) But since C and A::B are the same class, it obviously cannot be both nested and not nested. The only logical conclusion is that the class itself isn't nested.

    The defining feature of nested classes is that method lookup goes along two dimensions: up the inheritance chain, and outwards through the nesting. Ruby, like 99.9% of all OO languages, only supports the former. (In some sense, nested classes inherit not only the features of their superclass, but also the features of their surrounding class.)

    0 讨论(0)
  • 2021-02-12 14:16

    This is just for the lulz:

    class A
      def a
        puts "hello from a"
      end
    
      class B
        def b
          Module.nesting[1].new.a()
        end
      end
    end
    
    0 讨论(0)
  • 2021-02-12 14:26

    Well depending on your circumstances there is actually a solution, a pretty easy one at that. Ruby allows the catching of method calls that aren't captured by the object. So for your example you could do:

    def class A
      def a
        raise "hi" #can't be reached
      end
    
      class B
        def initialize()
          @parent = A.new
        end
    
        def b
          a() #does find method a.
        end
    
        def method_missing(*args)
          if @parent.respond_to?(method)
            @parent.send(*args)
          else
            super
          end
        end
      end
    end
    

    So then if you do this:

    A::B.new().b
    

    you get:

    !! #<RuntimeError: hi>
    

    It is probably an easier way to make something like a SubController that only handles certain activities, but can easily call basic controller methods (You would want to send in the parent controller as an argument in the initializer though).

    Obviously this should be used sparingly, and it can really get confusing if you use it all over the place, but it can be really great to simplify your code.

    0 讨论(0)
  • 2021-02-12 14:28

    If you want then nested class to extend the outer class, then do so:

    class Outer
    
      class Inner < Outer
        def use_outer_method
          outer_method("hi mom!")
        end
      end
    
      def outer_method(foo)
        puts foo
      end
    
    end
    
    foo = Outer::Inner.new
    foo.use_outer_method        #<= "hi mom"
    foo.outer_method("hi dad!") #<= "hi dad"
    
    0 讨论(0)
提交回复
热议问题