From: Robert Klemme Date: 2009-04-29T14:50:02+09:00 Subject: Re: Random Access using IO#pos in code blocks On 28.04.2009 22:32, Arun Kumar wrote: > Hello everyone, > I'm 20 days new to Ruby, please forgive if I make any mistakes. I'm > on a project where I'm indexing certain words in a text document. So > I'm also storing the file position where the word occurs. But the > Problem is: > The IO#pos points to the end of the file all the while... Below is > the code I'm working on: > > File.open(file_name) do |f| > > f.readlines("\r\n\r\n").each do |para| The reason is in the line above. > para.scan(/\b\w+\b/).each do |word| > > word = word.downcase.stem > if (!stoplist.include? word) && (!word.empty?) #excludes empty > and frequent words > > unless freq.has_key?(word) > freq[word] = [1,f.pos,file_name] # freq is a hash, that > stores an array containing index, position of word (THE PROBLEM).. > else > freq[word].to_a[0] += 1 > freq[word].to_a<< f.pos << file_name > end > > unless wfreq.has_key?(word) > wfreq[word] = [1,f.pos,file_name] > else > wfreq[word].to_a[0] += 1 > wfreq[word].to_a<< f.pos << file_name > end > > end > end > end > > > File.open(file_name+".yaml","w"){|f| YAML.dump(freq,f)} > > Also it would be great if someone told me the replacement for the > deprecated 'to_a' method used above :) Why do you convert an Array into an Array? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/