Topic: Should I use list or each or a regexp?

Let's say I have a simple guessing game. People have entered their guesses into the db via a form with letters delimited by commas.

Row/field 1-    a,p,p,l,e
Row/field 2-    r,o,c,k,y
Row/field 3-    t,e,s,t,y
etc...

& I want to return all rows that match 2 or more letters- ex. $var_correct_guess="r,i,g,h,t"

//With another language I would use something like:
var_letters="SELECT column_name FROM table_name
WHERE column_name IN  <? $var_correct_guess ?>"

//and then run it again to get the count
var_count="SELECT COUNT(*) FROM table_name
WHERE column_name IN  <? $var_correct_guess ?>"


//and then print it all out inside an if/else statement
<?php
echo "User" $user_id "got" $var_count "letters right. They were" $var_letters". Try again."
?>

I hope this doesn't read like gibberish--I am a beginner and the syntax is wrong but I think you get the idea. I'm sure RoR can do this better. I've looked at list and each in the ruby programming guide but I'm not sure if this is the proper way to do this example.

Any help would be greatly appreciated. Thanks in advance.

Re: Should I use list or each or a regexp?

I think I'd use a separate table "letters" that has_many "words".

If you're looking to make this your first sample app, go watch the ruby on rails screencast here: http://www.rubyonrails.org/screencasts -- follow along (actually do it), and think about what you would do in your app that's similar.  Plan out what you're going to do.

Re: Should I use list or each or a regexp?

A separate table of letters is a good option, I don't know if it is necessary though.

Is the answer/correct word known by Ruby when the users are making their guess? If so you can record how many letters are correct at the same time you save the guess to the database, this way you can easily query the best guesses.

I can go into more detail and give you some code if you need me to.

Railscasts - Free Ruby on Rails Screencasts

Re: Should I use list or each or a regexp?

No, the correct answer is not known at the time of their guess. I'd love it if you could post some code. I'm only up to page 100 or so in the agile dev book. Thanks.

Re: Should I use list or each or a regexp?

Since the correct answer is not known at the time of the guess it isn't possible to record their score at that time. This makes it difficult to query the database on their score later since the logic to determine their score has to be moved into the database query itself. Putting the letters into a separate table will make it easier to do this, but I don't think I could provide a query without doing testing.

If you aren't concerned about performance, keeping the score logic in Ruby would be easiest. For example, let's say you have a Guess model which stores one guess (string of letters). You can create a method in the model which returns the score of the guess. For example:

# in Guess model
def score(answer)
  score = 0
  # splitting by an empty string here will split each letter
  answer.split('').each do |letter|
    # assuming here that "letters" is the user's guess (string of letters separated by commas)
    score += 1 if letters.split(',').include? letter
  end
  score
end

The code can be made more concise, but I thought I would keep it like this so it's a little easier to understand. If you need me to explain any of that just let me know.

Edit: Actually it can be made much more concise and it's probably just as easy to read.

def score(answer)
  (answer.split('') & letters.split(',')).length
end

The "&" operator here will return an array which shares all of the common values between the two arrays. Grabbing the length of this will determine how many letters match the answer.

Last edited by ryanb (2007-01-17 18:34:14)

Railscasts - Free Ruby on Rails Screencasts

Re: Should I use list or each or a regexp?

I think I understand it. Thank you very much!!