From a file to an array of words


#1

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


#2

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

#3

Not a single but basically,

File.read( fname ).split(/\s+/)