Class: Nicolus::Combinaison

Inherits:
Object show all
Defined in:
lib/nicolus.rb

Overview

Usage

comb = Nicolus::Combinaison.new("input.csv", "output.csv", true, true)
comb.run

Instance Method Summary collapse

Constructor Details

#initialize(input_file, filename, inv = false, unicode = false) ⇒ Combinaison

Returns a new instance of Combinaison.



37
38
39
40
41
# File 'lib/nicolus.rb', line 37

def initialize(input_file, filename, inv=false, unicode=false)
  @input_file, @filename = input_file, filename
  @inv = inv
  @unicode = unicode
end

Instance Method Details

#create_listsObject

a,1,A b,2,B c,3,C

> [[a, b, c], [1, 2, 3], [A, B, C]]



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nicolus.rb', line 51

def create_lists
  list = []
  if (@unicode)
    content = IO.read(@input_file).utf16_to_iso
    content.split("\n").each do |line|
      columns = line.nicolus_split # chomp.gsub("\t", ',').split(',')
      list << columns
    end
  else
    IO.foreach(@input_file) do |line|
      columns = line.nicolus_split # chomp.gsub("\t", ',').split(',')
      list << columns
    end
  end
  # [[a, 1, A], [b, 2, B], [c, 3, C]] => [[a, b, c], [1, 2, 3], [A, B, C]]
  @listes = (Matrix.rows list).transpose.to_a
end

#create_resultObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nicolus.rb', line 69

def create_result
  # Too complex should take a look at ruby 1.9 array.combination() 
  # http://ruby-doc.org/core-1.9/classes/Array.src/M002238.html
  File.open(@filename, File::CREAT|File::RDWR|File::TRUNC) do |file|
    @listes.each do |line|
      line.each do |item|
        # unless(item.nil? or item.strip.empty?)
        unless(item.blank?)
          item.unquote!
          pos = @listes.index(line)
          sub_array = @listes - @listes[0, pos+1]
          sub_array.each do |sub_line|
            sub_line.each do |rest|
              # unless(rest.nil? or rest.strip.empty?)
              unless(rest.blank?)
                rest.unquote!
                file << "#{item} #{rest}\n" 
                file << "#{rest} #{item}\n" if(inverse?)
              end # unless(rest)
            end # sub_line
          end # sub_array
        end # unless(item)
      end # line.each
    end # @listes.each
  end # File.open
end

#inverse?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/nicolus.rb', line 43

def inverse?
  return @inv
end

#runObject

create_result()



96
97
98
99
# File 'lib/nicolus.rb', line 96

def run
  create_lists
  create_result
end