Class: IO

Inherits:
Object show all
Defined in:
lib/sixarm_ruby_ramp/io.rb

Overview

IO extensions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.readrows(filename, options = {}) ⇒ Array<Array<String>>

Reads the entire file specified by name as individual lines as with IO#readlines, and returns those lines in an array of rows, where each row is an array of fields.

Note: the col option is sent along to String#split, so can be a string or a regexp.

  • File#readline

  • File#readlines

  • File#readrow

Examples:

IO.readrows("my.tsv")
 => [["A1","B1","C1"],["A2","B2","C2"],["A3","B3","C3"]]

with options suitable for a file using TSV (Tab Separated Values)

IO.readrows("my.tsv", :row=>"\n", :col=>"\t") 

Parameters:

  • filename (String)
  • options (Hash) (defaults to: {})
    • Rows are separated by row option, which defaults to Ruby’s record separator $/ or “n”

    • Cols are separated by col option, which defaults to Ruby’s string split separator $; or “t”

Returns:

  • (Array<Array<String>>)

    an array of rows, where each row is an array of fields.

See Also:



32
33
34
35
36
# File 'lib/sixarm_ruby_ramp/io.rb', line 32

def IO.readrows(filename, options={})
  row_sep||=options[:row]||$/||"\n"
  col_sep||=options[:col]||$;||"\t"
  return IO.readlines(filename, row_sep).map{|line| line.chomp(row_sep).split(col_sep)}
end

Instance Method Details

#readrow(options = {}) ⇒ Array<String>

Read a line as with IO#readline and return the line as a row of fields.

Note: the col option is sent along to String#split, so can be a string or a regexp.

  • Rows are separated by row option, which defaults to Ruby’s record separator $/ or “n”

  • Cols are separated by col option, which defaults to Ruby’s string split separator $; or “t”

Examples:

file = File.new("my.tsv")
file.readrow() => ["A1","B1","C1"]
file.readrow() => ["A2","B2","C2"]
file.readrow() => ["A3","B3","C3"]]

with options suitable for a file using TSV (Tab Separated Values)

file = File.new("my.tsv")
file.readrow(:row=>"\n", :col=>"\t") => ["A1","B1","C1"] 
file.readrow(:row=>"\n", :col=>"\t") => ["A2","B2","C2"] 
file.readrow(:row=>"\n", :col=>"\t") => ["A3","B3","C3"] 

Parameters:

  • options (Hash) (defaults to: {})

Returns:

See Also:

  • File#readline
  • File#readlines
  • File#readrows


65
66
67
68
69
# File 'lib/sixarm_ruby_ramp/io.rb', line 65

def readrow(options={})
  row_sep||=options[:row]||$/||"\n"
  col_sep||=options[:col]||$;||"\t"
 return readline(row_sep).chomp(row_sep).split(col_sep)
end