In this example code where I have child classes (Bar, Baz) inheriting the class methods of the parent (Foo), how might I ensure @foo is only created once across all children?
class Foo
def self.foo
# only want @foo to be set once across any child classes
# that may call this inherited method.
@foo ||= expensive_operation
end
end
class Bar < Foo
def self.bar
self.foo + 'bar'
end
end
class Baz < Foo
def self.baz
self.foo + 'baz'
end
end
self.foo + 'bar'can be writtenfoo + barasselfisBarwhen that is executed. Same forBaz.