ruby - Class accessor return nil -


class foo   class << self     attr_accessor :var   end end  class bar < foo   var = "bar"   p var   # print "bar" end  p bar.var # print nil 

why bar.var not return "bar"?

how can add getter/setter class variables?

class bar < foo   var = "bar" # assignment local variable, not accessor end 

use self tell ruby want call method, not create local variable.

class foo   class << self     attr_accessor :var   end end  class bar < foo   self.var = "bar"   var # => "bar" end  bar.var # => "bar" 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -