supergalactic supergalactic - 4 years ago 64
Ruby Question

Whats the best way to put a small ruby app online?

I have a small ruby application I wrote that's an anagram searcher. It's for learning ruby, but I would like to put it up online for personal use. I have some experience with Rails, and many here have recommended Sinatra. I'm fine with either, but I cannot find any information on how to use a text file instead of a database.

The application is quite simple, validates against a text file of a word list, then finds all anagrams. I have been assuming that this should be quite simple, but I'm stuck on importing that textfile into Rails (or Sinatra if i choose that way). In the Rails project, I have placed the textfile in the

lib
directory.

Unfortunately, even though the path appears to be correct in Rails, I get an error:

no such file to load -- /Users/court/Sites/cvtest/lib/english.txt


(
cvtest
is the name of the rails project)

Here is the code. It works great by itself:

file_path = '/Users/court/Sites/anagram/dictionary/english.txt'

input_string = gets.chomp


# validate input to list
if File.foreach(file_path) {|x| break x if x.chomp == input_string}

#break down the word
word = input_string.split(//).sort

# match word
anagrams = IO.readlines(file_path).partition{
|line| line.strip!
(line.size == word.size && line.split(//).sort == word)
}[0]

#list all words except the original
anagrams.each{ |matched_word| puts matched_word unless matched_word == input_string }


#display error if
else
puts "This word cannot be found in the dictionary"
end

Answer Source

Factor the actual functionality (finding the anagrams) into a method. Call that method from your Web app.

In Rails, you'd create a controller action that calls that method instead of ActiveRecord. In Sinatra, you'd just create a route that calls the method. Here's a Sinatra example:

get '/word/:input'
  anagrams = find_anagrams(params[:input])
  anagrams.join(", ")
end

Then, when you access the http://yourapp.com/word/pool, it will print "loop, polo".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download