title: Cache class method instance variable in Ruby
body: <p>In this example code where I have child classes (<code>Bar</code>, <code>Baz</code>) inheriting the class methods of the parent (<code>Foo</code>), how might I ensure <code>@foo</code> is only created once across all children?</p>
  
  <pre><code>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 &lt; Foo
    def self.bar
      self.foo + 'bar'
    end
  end
  
  class Baz &lt; Foo
    def self.baz
      self.foo + 'baz'
    end
  end
  </code></pre>