Class: RuneRb::Core::Buffer
- Inherits:
-
Object
- Object
- RuneRb::Core::Buffer
- Defined in:
- lib/rune/core/buffer.rb
Overview
A Buffer encapsulates raw data in a String instance. Depending on the mode, the buffer can be read from or written to.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Adds raw data to the buffer.
-
#empty? ⇒ Boolean
Is the Buffer#data empty?.
-
#initialize(mode: 'rw') ⇒ Buffer
constructor
Constructs a new Buffer instance.
-
#length ⇒ Integer
Returns the limit of the buffer.
-
#mutate(value, mutation) ⇒ Object
Mutates the value according to the passed mutation.
-
#readable? ⇒ Boolean
Is the <Buffer> instance readable?.
-
#snapshot ⇒ String
Returns a snapshot of the buffer.
-
#writeable? ⇒ Boolean
Is the <Buffer> instance writeable?.
Constructor Details
#initialize(mode: 'rw') ⇒ Buffer
Constructs a new Buffer instance.
8 9 10 11 12 13 14 15 |
# File 'lib/rune/core/buffer.rb', line 8 def initialize(mode: 'rw') @data = String.new @mode = mode raise StandardError, 'Buffer mode must include "r" or "w".' unless @mode.include?('r') || @mode.include?('w') enable_readable if @mode.include?('r') enable_writeable if @mode.include?('w') end |
Instance Method Details
#<<(data) ⇒ Object
Adds raw data to the buffer. Flips the buffer if it is readable.
37 38 39 |
# File 'lib/rune/core/buffer.rb', line 37 def <<(data) @data << data end |
#empty? ⇒ Boolean
Is the Buffer#data empty?
25 26 27 |
# File 'lib/rune/core/buffer.rb', line 25 def empty? @data.empty? end |
#length ⇒ Integer
Returns the limit of the buffer.
31 32 33 |
# File 'lib/rune/core/buffer.rb', line 31 def length @data.bytesize end |
#mutate(value, mutation) ⇒ Object
Mutates the value according to the passed mutation
56 57 58 59 60 61 62 63 64 |
# File 'lib/rune/core/buffer.rb', line 56 def mutate(value, mutation) case mutation when :STD then value when :ADD then value + 128 when :NEG then -value when :SUB then value - 128 else mutate(value, :STD) end end |
#readable? ⇒ Boolean
Is the <Buffer> instance readable?
43 44 45 |
# File 'lib/rune/core/buffer.rb', line 43 def readable? @mode.include?('r') end |
#snapshot ⇒ String
Returns a snapshot of the buffer.
19 20 21 |
# File 'lib/rune/core/buffer.rb', line 19 def snapshot @data.dup end |
#writeable? ⇒ Boolean
Is the <Buffer> instance writeable?
49 50 51 |
# File 'lib/rune/core/buffer.rb', line 49 def writeable? @mode.include?('w') end |