Class: TEALrb::Scratch
- Inherits:
-
Object
- Object
- TEALrb::Scratch
- Defined in:
- lib/tealrb/scratch.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
-
#initialize(contract) ⇒ Scratch
constructor
A new instance of Scratch.
- #reserve(slot) ⇒ Object
- #store(key, value = @contract.teal) ⇒ Object
- #unreserve(slot) ⇒ Object
Constructor Details
#initialize(contract) ⇒ Scratch
Returns a new instance of Scratch.
5 6 7 8 9 10 |
# File 'lib/tealrb/scratch.rb', line 5 def initialize(contract) @open_slots = (0..256).to_a @named_slots = {} @values = {} @contract = contract end |
Instance Method Details
#[](key) ⇒ Object
12 13 14 15 |
# File 'lib/tealrb/scratch.rb', line 12 def [](key) @contract.teal << "load #{@named_slots[key]} // #{key}" @values[key] end |
#[]=(key, value) ⇒ Object
17 18 19 |
# File 'lib/tealrb/scratch.rb', line 17 def []=(key, value) store(key, value) end |
#delete(key) ⇒ Object
26 27 28 29 |
# File 'lib/tealrb/scratch.rb', line 26 def delete(key) @values.delete(key) @open_slots << @named_slots.delete(key) end |
#reserve(slot) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/tealrb/scratch.rb', line 31 def reserve(slot) name = @named_slots.key(slot) raise ArgumentError, "Attempted to reserve a slot (#{slot}) that corresponds to a named slot (#{name})" if name @open_slots.delete slot end |
#store(key, value = @contract.teal) ⇒ Object
21 22 23 24 |
# File 'lib/tealrb/scratch.rb', line 21 def store(key, value = @contract.teal) @values[key] = value @contract.teal << "store #{@named_slots[key] ||= @open_slots.shift} // #{key}" end |
#unreserve(slot) ⇒ Object
38 39 40 |
# File 'lib/tealrb/scratch.rb', line 38 def unreserve(slot) @open_slots << slot end |