Class: MemoryIO::IO
- Inherits:
-
Object
- Object
- MemoryIO::IO
- Defined in:
- lib/memory_io/io.rb
Overview
Main class to use MemoryIO.
Instance Attribute Summary collapse
- #stream ⇒ #pos=, ... readonly
Instance Method Summary collapse
-
#initialize(stream) ⇒ IO
constructor
Instantiate an IO object.
-
#read(num_elements, from: nil, as: nil, force_array: false) ⇒ String, ...
Read and convert result into custom type/stucture.
-
#rewind ⇒ 0
Set
streamto the beginning.
Constructor Details
#initialize(stream) ⇒ IO
Instantiate an MemoryIO::IO object.
15 16 17 |
# File 'lib/memory_io/io.rb', line 15 def initialize(stream) @stream = stream end |
Instance Attribute Details
#stream ⇒ #pos=, ... (readonly)
6 7 8 |
# File 'lib/memory_io/io.rb', line 6 def stream @stream end |
Instance Method Details
#read(num_elements, from: nil, as: nil, force_array: false) ⇒ String, ...
Note:
This method’s arguments and return value are different with ::IO#read. Check documents and examples.
Read and convert result into custom type/stucture.
98 99 100 101 102 103 104 105 106 |
# File 'lib/memory_io/io.rb', line 98 def read(num_elements, from: nil, as: nil, force_array: false) stream.pos = from if from return stream.read(num_elements) if as.nil? conv = to_proc(as, :read) # TODO: handle eof ret = Array.new(num_elements) { conv.call(stream) } ret = ret.first if num_elements == 1 && !force_array ret end |
#rewind ⇒ 0
Set stream to the beginning. i.e. invoke stream.pos = 0.
112 113 114 |
# File 'lib/memory_io/io.rb', line 112 def rewind stream.pos = 0 end |