Forum: Ruby from a file to an array of words

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
64bc95dd4f1257d4a061a35a38e6e26d?d=identicon&s=25 ngoc (Guest)
on 2005-11-28 13:23
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-11-28 13:47
(Received via mailing list)
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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-11-28 13:51
(Received via mailing list)
Not a single but basically,

  File.read( fname ).split(/\s+/)
This topic is locked and can not be replied to.