Class: Merger
- Inherits:
-
Object
- Object
- Merger
- Defined in:
- lib/merger.rb
Instance Method Summary collapse
- #get_line(stream, parse_cols = true) ⇒ Object
-
#initialize ⇒ Merger
constructor
A new instance of Merger.
- #merge! ⇒ Object
Constructor Details
#initialize ⇒ Merger
Returns a new instance of Merger.
2 3 4 5 6 7 8 9 |
# File 'lib/merger.rb', line 2 def initialize @filename1 = ENV["filename1"] @filename2 = ENV["filename2"] @output_filename = ENV["output_filename"] @sort_column = ENV["sort_column"].to_i @sort_as_int = ENV["sort_as_int"] == "true" @column_separator = ENV["column_separator"] end |
Instance Method Details
#get_line(stream, parse_cols = true) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/merger.rb', line 43 def get_line(stream, parse_cols = true) line = stream.gets return [nil, nil] if line.nil? return [line, nil] unless parse_cols cols = line.chomp.split(@column_separator, @sort_column + 2) cols[@sort_column] = cols[@sort_column].to_i if @sort_as_int return [line, cols[@sort_column]] end |
#merge! ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/merger.rb', line 11 def merge! outfile = File.open(@output_filename, "w") f1 = File.open(@filename1) f2 = File.open(@filename2) f1_line, f1_col = self.get_line(f1) f2_line, f2_col = self.get_line(f2) while !f1_line.nil? and !f2_line.nil? if f1_col < f2_col outfile.print f1_line f1_line, f1_col = self.get_line(f1) else outfile.print f2_line f2_line, f2_col = self.get_line(f2) end end while !f1_line.nil? outfile.print f1_line f1_line, f1_col = self.get_line(f1, false) end while !f2_line.nil? outfile.print f2_line f2_line, f2_col = self.get_line(f2, false) end f1.close f2.close outfile.close end |