ruby - How do I give my instance variable a getter? -
i have instance variable want set value if it's nil when requested. correct, or cause problems?
class attr_accessor :var def initialize @var end def var if @var == nil #something determines value var end @var end end
class attr_writer :var def initialize @var end def var @var ||= 12 end end foo = something.new foo.var # => 12 foo.var = 15 foo.var # => 15
Comments
Post a Comment