質問

I am writing a small project in ruby that takes all of the words from a website and then sorts them short to long.

To verify that what gets sorted is actually valid english I am comparing an array of scraped words to the basic unix/osx words file.

The method to do this is the spell_check method. The problem is that when used on small arrays is works fine, but with larger ones it will let non-words through. Any ideas?

def spell_check (words_array)
  dictionary = IO.read "./words.txt"
  dictionary = dictionary.split 
  dictionary.map{|x| x.strip }

  words_array.each do |word|
    if !(dictionary.include? word)
      words_array.delete word
    end 
  end 

  return words_array
end 
役に立ちましたか?

解決

I simplified your code, maybe this will work?

def spell_check(words)
  lines = IO.readlines('./words.txt').map { |line| line.strip }
  words.reject { |word| !lines.include? word }
end

I noticed that you were trying to modify the words_array while you were simultaneously iterating over it with each:

words_array.each do |word|
  if !(dictionary.include? word)
    words_array.delete word # Delete the word from words_array while iterating!
  end 
end

I'm not sure if this is the case in Ruby, but in other programming languages like Java and C#, trying to modify a collection, while you're iterating over it at the same time, invalidates the iteration, and either produces unexpected behavior, or just throws an error. Maybe this was your problem with your original code?

Also, the return statement was unnecessary in your original code, because the last statement evaluated in a Ruby block is always returned (unless there's an explicit return that precedes the last statement). It's idiomatic Ruby to leave it out in such cases.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top