Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a method opening and appending to a file as so:

def writeFile(ofile,number)
  File.open(ofile, 'a') { |file| file.puts(number) }
end

Which is called after line processing by another method.

def method(ofile, ifile, number)
  File.foreach("#{ifile}") do |line|
    if number > 3
      writeFile(ofile, number) 
  end
end

My question is, should I just get rid of the writeFile method? It seems wrong to me. It keeps opening the file to write to it, when I should only open it once then write when needed.

share|improve this question
1  
btw, it's more conventional in ruby to name methods like method_name rather than methodName. – Nat Feb 16 at 15:17
As an aside, method is an existing method defined on Object, and you are trampling it. – steenslag Feb 26 at 22:15

1 Answer

up vote 2 down vote accepted

If this is the only place the writeFile method is called then you might as well put its single line into the other method in place of the method call.

As for the wisdom of appending to a file incrementally instead of building up a string to write all at once: It's a little extra work to repetitively open a file, but it might be worth it to to save on memory if you want to write a lot of data.

It very much depends on the nature of the data you're working with. It's best not to build up hundreds of MB of string(s) in memory if you can help it, but if you're just dealing with thousands of very small pieces of data (like numbers) then it's best not to open the file for each one.


EDIT:

that said you can also keep both files open the whole time like:

output = File.open(ofile, 'w')
File.foreach(ifile) { |line| output.puts line if line.length > 100 }
output.close

or

File.open(ofile, 'w') do |f|
  File.foreach(ifile) do |line|
    f.puts line if line.length > 100
  end
end
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.