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

后端 未结 7 2205
-上瘾入骨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: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
    

提交回复
热议问题