Sha256: f6b04d960c6d905278d0ad08e135eaec54c343806a1ccb067fe093bbae585be5

Contents?: true

Size: 605 Bytes

Versions: 4

Compression:

Stored size: 605 Bytes

Contents

class Module

  # Create a memoized method. This method has been popularized by RSpec.
  #
  #   class LetExample
  #     let(:seed) { rand }
  #   end
  #
  #   eg = LetExample.new
  #   eg.seed == eg.seed
  #
  # CREDIT: Howard Yeh
  #
  # @uncommon
  #   require 'facets/module/let'
  #
  def let(var,&block)
    name = "@#{var}"
    self.class_eval do
      define_method(var) do
        if instance_variable_defined?(name)
          instance_variable_get(name)
        else
          val = self.instance_eval(&block)
          instance_variable_set(name,val)
        end
      end
    end
  end

end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/module/let.rb
facets-3.1.0 lib/core/facets/module/let.rb
facets-3.0.0 lib/core/facets/module/let.rb
facets-2.9.3 lib/core/facets/module/let.rb