Class: ConverterTxt

Inherits:
Converter show all
Defined in:
lib/source/Converter_txt.rb

Instance Method Summary collapse

Instance Method Details

#read_file(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/source/Converter_txt.rb', line 5

def read_file(data)
  result = []
  file_content = data.split("\n")
  file_content.each do |line|
    hash = {}
    puts(line)
    pairs = line.split(',').map{|pair| pair.gsub(/\s+/, '').split(':')}
    pairs.each do |pair|
      hash[pair[0].to_sym]=(pair[0]=="id") ? pair[1].to_i : pair[1]
    end
    result << hash
  end
  result
end

#write_file(hash_students) ⇒ Object



20
21
22
23
24
# File 'lib/source/Converter_txt.rb', line 20

def write_file(hash_students)
  string_arr = hash_students.map do |hash|
    hash.map{|k,v| "#{k}:#{v}"}.join(',')
  end.join("\n")
end