Class: Keisan::AST::Cache
- Inherits:
-
Object
- Object
- Keisan::AST::Cache
- Defined in:
- lib/keisan/ast/cache.rb
Instance Method Summary collapse
- #fetch_or_build(string) ⇒ Object
- #has_key?(string) ⇒ Boolean
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize ⇒ Cache
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
20 21 22 |
# File 'lib/keisan/ast/cache.rb', line 20 def has_key?(string) @cache.has_key?(string) end |