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

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -