Recommand · November 22, 2021 0

how to combine 2 file text with join line in ruby

Please help me how to do with this code. and what should i do with that code:

file1.txt       file2.txt
  aaa             111
  bbb             222
  ccc             333
  ddd             444

and the result just show like this

  aaa
  bbb
  ccc
  ddd
  111
  222
  333
  444

but what i want is

aaa|111
bbb|222
ccc|333
ddd|444

Here is my code

f1 = File.readlines('./file1.txt')
f2 = File.readlines('./file2.txt')

File.open('result.txt', 'w') do |output_file|
  f1.each_with_index do |elem, i|
    output_file.puts "#{elem} #{f2[i]}"
  end
end