Class: LRU::Cache
- Inherits:
-
Object
- Object
- LRU::Cache
- Defined in:
- lib/lru/cache.rb,
lib/lru/cache/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
Instance Method Summary collapse
- #[](key) ⇒ Object (also: #get)
- #[]=(key, value) ⇒ Object (also: #set)
-
#initialize(max, hash = {}) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(max, hash = {}) ⇒ Cache
Returns a new instance of Cache.
14 15 16 17 |
# File 'lib/lru/cache.rb', line 14 def initialize(max, hash={}) @max = max @content = hash end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
12 13 14 |
# File 'lib/lru/cache.rb', line 12 def content @content end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
12 13 14 |
# File 'lib/lru/cache.rb', line 12 def max @max end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
19 20 21 |
# File 'lib/lru/cache.rb', line 19 def [](key) content[key] = content.delete(key) if content.has_key? key end |
#[]=(key, value) ⇒ Object Also known as: set
24 25 26 27 28 |
# File 'lib/lru/cache.rb', line 24 def []=(key, value) content.delete key if content.has_key? key content.delete(content.first.first) if content.length == max content[key] = value end |