From: Robert Klemme Date: 2003-09-19T17:37:22+09:00 Subject: Re: Why IO#readlines does'nt accept a Regexp? "Sabby and Tabby" schrieb im Newsbeitrag news:f5a79bf2.0309181614.1b288fb7@posting.google.com... > "Robert Klemme" wrote: > > > For large Files this is more efficient: > > > > words=[] > > IO.foreach("myfile") do |line| > > words.push( *line.scan( /\w+/oi ) ) > > end > > The /oi modifiers aren't necessary. Granted. I just grew used to putting "o" in there whenever the rx doesn't change over time. Kind of a documentation thingy. > > If you have many repeating words you can save even more mem: > > > > cache = Hash.new {|h,k| h[k]=k} > > words = [] > > > > IO.foreach("myfile") do |line| > > words.push( *( line.scan( /\w+/oi ).map {|w| cache[w]} ) ) > > end > > The #map isn't doing what you think it is doing. To remove repeating > words from the list: It does exactly what I think it's doing. :-) I don't want to remove repeated words from the list but replace all identical strings with the same *instance* to save memory. map fit's the job perfectly. Of course, you could use collect also... :-) Regards robert