how to access a class variable of outer class from inner class in ruby

依然范特西╮ 提交于 2019-12-04 07:31:04

The only way to access this class variable is via an accessor method

class A
   def self.lock
     @@lock ||= Monitor.new
   end

   class B
     def method
       A.lock.synchronize
         puts "xxxxx"
       end
     end
   end
 end

I don't think you can without defining an accessor.

Class B is lexically scoped inside of A, so its real name is A::B and various other things are different.

But it's not a child or any other sort of derived class, so it doesn't actually have any special rights to see private or protected or otherwise local elements of A.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!