Ok, I have this ruby script which opens a file and passes each line to the line_parser method. This method simply changes tabs for a couple of spaces. Here it is:
$lines = []
def line_parser(line)
line.gsub! /\t/, ' '
$lines[$lines.length] = line
end
f = File.open(ARGV[0], 'r').each { |line| line_parser(line) }
f.close
f = File.open(ARGV[0], 'w')
$lines.each { |line| f.puts line}
f.close
As you can see it has two loops; one loop to iterate over the lines of text inside the file an place them inside of an array. And the other loop writes the file all over again with the new lines from the recently created array.
My question is simple I think: How can I refactor this snippet so that I do all the steps described above inside one loop only?
I know it can be done, I just have not been able to do it with my current ruby knowledge.
Thanks in advance!
sed -i 's/\t/ /g' yourfilewould do the trick.