Class: Tracksperanto::ExtIO

Inherits:
IOWrapper show all
Defined in:
lib/tracksperanto/ext_io.rb

Overview

Many importers use this as a standard. This works like a wrapper for any IO object with a couple extra methods added. Note that if you use this in an importer you need to wrap the incoming input with it yourself (that is, an IO passed to the import module will NOT be wrapped into this already)

io = ExtIO.new(my_open_file)
io.gets_non_empty #=> "This is the first line after 2000 linebreaks"

Constant Summary

Constants inherited from IOWrapper

IOWrapper::IO_METHODS

Instance Attribute Summary

Attributes inherited from IOWrapper

#backing_buffer

Instance Method Summary collapse

Constructor Details

#initialize(with) ⇒ ExtIO

Returns a new instance of ExtIO.



9
10
11
# File 'lib/tracksperanto/ext_io.rb', line 9

def initialize(with)
  @backing_buffer = with
end

Instance Method Details

#gets_and_stripObject

Similar to IO#gets however it will also strip the returned result. This is useful for doing

while non_empty_str = io.gets_and_strip

because you don’t have to check for io.eof? all the time or see if the string is not nil



17
18
19
20
# File 'lib/tracksperanto/ext_io.rb', line 17

def gets_and_strip
  s = gets
  s ? s.strip : nil
end

#gets_non_emptyObject

Similar to IO#gets but it skips empty lines and the first line returned will actually contain something



23
24
25
26
27
28
29
30
# File 'lib/tracksperanto/ext_io.rb', line 23

def gets_non_empty
  until eof?
    line = gets
    return nil if line.nil?
    s = line.strip
    return s unless s.empty?
  end
end