thank you Chris!

i realized, after posting that this needed to be in the Sinatra forum,
but you responded so that is good!

I am still very new to RUBY and coding in general....so i am doing
things the long way. It seems here that you have condensed five lines
into one line?  That's amazing...

I replaced my for loop, and

my comparison line :

lettername = 'letter' + String(i)
lettername = lettername.to_sym

 if(params[lettername] == letter)
   lettername = letter

with:
  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

But I must be missing something. I am getting an error message that
says it doesn't recognize "value"...

Thanks a million for taking the time out to answer me...so much
appreciated.

Best,

Robin
On Oct 29, 8:16 pm, Chris Corbyn <[email protected]> wrote:
> 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 [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to