Class: Keisan::AST::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/keisan/ast/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



4
5
6
# File 'lib/keisan/ast/cache.rb', line 4

def initialize
  @cache = {}
end

Instance Method Details

#fetch_or_build(string) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/keisan/ast/cache.rb', line 8

def fetch_or_build(string)
  return @cache[string] if @cache.has_key?(string)

  build_from_scratch(string).tap do |ast|
    unless frozen?
      # Freeze the AST to keep it from changing in the cache
      ast.freeze
      @cache[string] = ast
    end
  end
end

#has_key?(string) ⇒ Boolean

Returns:



20
21
22
# File 'lib/keisan/ast/cache.rb', line 20

def has_key?(string)
  @cache.has_key?(string)
end