I have a small ruby application that I wrote that is anagram seeker. This is for learning ruby, but I would like to put it online for personal use. I have experience with Rails, and many of them have recommended Sinatra. I am fine too, but I cannot find any information on how to use a text file instead of a database.
The application is quite simple, checks the text file of the word list, then finds all the anagrams. I suppose this should be pretty simple, but I'm stuck on importing this text file into Rails (or Sinatra, if I choose that). In a Rails project, I put a text file in a directory lib.
Unfortunately, even though the path in Rails is correct, I get an error:
no such file to load -- /Users/court/Sites/cvtest/lib/english.txt
( cvtest- name of the rails project)
Here is the code. It works great on its own:
file_path = '/Users/court/Sites/anagram/dictionary/english.txt'
input_string = gets.chomp
if File.foreach(file_path) {|x| break x if x.chomp == input_string}
word = input_string.split(//).sort
anagrams = IO.readlines(file_path).partition{
|line| line.strip!
(line.size == word.size && line.split(//).sort == word)
}[0]
anagrams.each{ |matched_word| puts matched_word unless matched_word == input_string }
else
puts "This word cannot be found in the dictionary"
end
source
share