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.)
···
On Feb 14, 7:31 am, "Lars Olsson" <l...@lassoweb.nu> wrote:
Hi list!
What's the best way to remedy the error/warning that the following
code produces?
class Parent
def self.foo()
module_eval("def foo(); @foo; end")
module_eval("def foo=(newfoo); @foo = newfoo; end")
end
end
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.