end
I dunno what your criteria is for ‘best’, but this fixes it:
module_eval(“def foo(); @foo ||= nil; end”)
(Though that runs over values of false with nil.)
Thanks, the worked flawlessly. I just added an else clause so that I
can have other default values than nil.
class Parent
def self.foo()
module_eval(“def foo(); if defined?(@foo) then @foo else nil
end; end”)
module_eval(“def foo=(newfoo); @foo = newfoo; end”)
end
end
I dunno what your criteria is for ‘best’, but this fixes it:
module_eval(“def foo(); @foo ||= nil; end”)
(Though that runs over values of false with nil.)
That’s what I do also; just be careful however not to refer to the
instance variable from within the class; use the accessor method.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.