Write in a text file the result of a command in Ruby

1

I am trying to write the result of a command in a text file but I can not write anything. This is my code

f directorio2 == nil
    directorio2 = 'pwd'
    directorio2 = directorio2.to_s
end

puts "#{directorio2}"
system "diff -rs #{directorio1} #{directorio2} > Compara.txt"
system "sed '/Sólo/d' Compara.txt"

For this case, I want to compare the files of 2 files and if you do not tell me a second file, this will be the current route. The error is given when doing

directorio2 = 'pwd'

and then when doing

system "diff -rs #{directorio1} #{directorio2} > Compara.txt"

does the first part perfectly, that is, it does the diff -rs, but I can not write it in the text file

    
asked by M.use 03.05.2018 в 14:06
source

1 answer

1

The problem is in the result of pwd , which adds a character of new line \n at the end, therefore diff does not recognize the directory; to fix it simply add .strip at the end:

if directorio2 == nil
  directorio2 = 'pwd'.strip
end

Also, you do not need to use .to_s , the result of pwd is a String .

Not related to your question, but your code could be modified a bit to get closer to ruby's programming style:

directorio2 = 'pwd'.strip if directorio2.nil?

Or, using the || operator:

directorio2 = directorio2 || 'pwd'.strip
    
answered by 03.05.2018 / 16:14
source