understand self for attr_accessor class method

后端 未结 5 1884
礼貌的吻别
礼貌的吻别 2021-01-12 18:03
class Test
  class << self
    attr_accessor :some

    def set_some
      puts self.inspect
      some = \'some_data\'
    end
    def get_some
      puts sel         


        
5条回答
  •  青春惊慌失措
    2021-01-12 18:43

    some = :foo makes ruby think it should create a new local variable with name some. If you want to call some=(), you have to use an explicit reciever - as in self.some = :foo. I once lost a bet on that... :-/

提交回复
热议问题