Class: Koara::Io::StringReader

Inherits:
Object
  • Object
show all
Defined in:
lib/koara/io/stringreader.rb

Instance Method Summary collapse

Constructor Details

#initialize(text = '') ⇒ StringReader

Returns a new instance of StringReader.



5
6
7
8
# File 'lib/koara/io/stringreader.rb', line 5

def initialize(text='')
  @text = text
  @index = 0
end

Instance Method Details

#read(buffer, offset, length) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/koara/io/stringreader.rb', line 10

def read(buffer, offset, length)
  slice = @text.slice(@index, @text.length)

  if @text != '' && slice && slice.length > 0
    characters_read = 0
    0.upto(length - 1) do |i|
      c = @text.slice(@index + i)
      if c

        buffer[offset + i] = c
        characters_read += 1
      end
    end
    @index += length
    return characters_read
  end
  -1
end