Thank you, Robert.
Your welcome!
The links were most helpful.
Good!
I need to read char substrings from a file that has no line breaks in
it.
Note that you can use a different separator character:
File.foreach data_file, "SEPARATOR" do |entry|
puts entry
end
The same works for File.each:
File.open... do |io|
io.each "SEPARATOR" do |entry|
...
end
end
Is the code below the best way to do it?
File.foreach(dataFile) do |line|
puts line
end
Will the "line" var give the entire text in the file?
No, because this will try to load lines separated by \n. If you want
the whole text of the file in one go you can do
contents = File.read data_file
However, if the file is large, a blocked approach usually yields better results:
File.open data_file do |io|
buffer = ""
while io.read(1024, buffer)
buffer.each_char {|ch| p ch} # note, ch is really a String of length 1
end
end
Another question (this is posted on the link you gave)
Instead of doing File.foreach(dataFile) can I not do
IO.foreach(dataFile) do |line|?
Yes, File and IO are interchangeable here. Personally I prefer to use
File when dealing with files just for documentation reasons.
Kind regards
robert
···
2010/8/24 Rajarshi Chakravarty <raj_plays@yahoo.com>:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/