I want to compare two text(html) files. “diff” command in Linux compares
only by line.
In Ruby, I can use “File.readlines” and “split” later. Is there a single
function(method) to go from a file to an array of words.
Thanks
ngoc wrote:
I want to compare two text(html) files. “diff” command in Linux
compares only by line.
In Ruby, I can use “File.readlines” and “split” later. Is there a
single function(method) to go from a file to an array of words.
What is a word in your context? If you want an array of tokens
consisting
only of word characters you can do
words = File.read(file_name).scan(/\w+/)
Note though that this is memory intensive for large files. This is a
bit
more efficient:
words = File.open(file_name) do |io|
io.inject([]) {|w, line| w.concat( line.scan( /\w+/ ) )}
end
Kind regards
robert
Not a single but basically,
File.read( fname ).split(/\s+/)