Sha256: df381c257fd230a9375cfb244ce583c533e9f096466dc741b2f0f4b21d06ea8e

Contents?: true

Size: 1.09 KB

Versions: 12

Compression:

Stored size: 1.09 KB

Contents

# -*- encoding : utf-8 -*-
# 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"
class Tracksperanto::ExtIO < Tracksperanto::IOWrapper
  def initialize(with)
    @backing_buffer = with
  end
  
  # 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
  def gets_and_strip
    s = gets
    s ? s.strip : nil
  end
  
  # Similar to IO#gets but it skips empty lines and the first line returned will actually contain something 
  def gets_non_empty
    until eof?
      line = gets
      return nil if line.nil?
      s = line.strip
      return s unless s.empty?
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
tracksperanto-3.5.9 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.8 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.7 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.6 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.5 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.4 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.2 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.1 lib/tracksperanto/ext_io.rb
tracksperanto-3.5.0 lib/tracksperanto/ext_io.rb
tracksperanto-3.4.1 lib/tracksperanto/ext_io.rb
tracksperanto-3.4.0 lib/tracksperanto/ext_io.rb
tracksperanto-3.3.13 lib/tracksperanto/ext_io.rb