Hi Robin,

This is quite unrelated to DataMapper and more related to programming in 
general.  I'll answer anyway.

I'm not sure if you want critique on the code itself, but it struck me when I 
opened that gist just how un-ruby-esque the code is.  I looks like JavaScript 
or something at a first glance.

For example, creating markup with:

  form = ''
  form += '<form … >'
  form += '<input …>'
  form += ' …. '
  form += '</form>'

Is needlessly verbose and hard to read.  Side-stepping the fact that Sinatra 
allows you to use template engines like ERB or HAML, you can immediately 
improve the current code simply by replacing the repeated concatenation with 
here-doc:

  form = <<-HTML
    <form … >
      <input …. >
      …..
    </form>
  HTML

Also, in ruby, using for-loops is generally avoided in favour of #each:

  for letter in a.word.split("")
    output +="<input type='text' name ='letter[#{i}]' value='' size='1'/>"
    i = i+1
  end

Becomes

  a.word.split("").each do |letter|
    output +="<input type='text' name ='letter[#{i}]' value='' size='1'/>"
    i = i+1
  end

You can also remove the need for the temporary variable 'i' by using a more 
appropriate iterator method:

  a.word.split("").each_with_index do |letter, i|
    output +="<input type='text' name ='letter[#{i}]' value='' size='1'/>"
  end

With the for loop removed and us now looking at what methods ruby offers for 
working with its data structures, I *think* the logic you're looking for is a 
letter-by-letter comparison with the expected word, which #zip lends itself to:

  a.word.split("").zip(params[:letter].values).each_with_index do |(expected, 
got), idx|
    output += "<input type='text' name ='letter[#{i}]' value='#{expected == got 
? expected : ''}' size='1'/>"
  end


Regards,

Chris


On 30/10/2011, at 9:10 AM, Robin Reid wrote:

> hi all,
> 
> i am writing a puzzle game in sinatra and using datamapper. i am
> extremely stuck in the last part of my game. The game has 2 parts: 1)
> someone can create a word puzzle and send it to their friends 2) that
> puzzle player can try to fill it out and see if they get he correct
> answer.
> 
> in the final step of the puzzleplayers game, he/she must fill in boxes
> with the letters that they are guessing. I want to be able to compare,
> immediately as they enter, if their guessed letter, matches the letter
> stored by the puzzle creator. If it matches, then the input letters
> will stay in the box, if they don't it will wipe out, they will have
> to try again.  i don't know how to do this.
> 
> my code is here...if anyone is around and cares to take a crack at
> helping me. I would greatly appreciate it.
> 
> https://gist.github.com/39bd78d70018e3438080
> 
> thanks!
> 
> robin reid
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "DataMapper" group.
> To post to this group, send email to datamapper@googlegroups.com.
> To unsubscribe from this group, send email to 
> datamapper+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/datamapper?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to datamapper@googlegroups.com.
To unsubscribe from this group, send email to 
datamapper+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to