-------- Original-Nachricht --------
> Datum: Sun, 18 Oct 2009 20:21:02 +0900
> Von: Matt Beckley <greenlizards / yahoo.com>
> An: ruby-talk / ruby-lang.org
> Betreff: Need help comparing Array data

> Trying to write code for rock paper scissors game and read in two values
> from people playing. Take those values and determine the outcome. As I
> understand an array you are able to compare values with the <=> and I
> think this is the big jump I need to making things work. I would have
> something like this:
> 
>  a = [rock,paper,scissors] #Comparison array
>  b = [userinput1 , userinput2] #User store .a values
> 
> 
>  if userinput1 < userinput2   #loop for showing who wins
>  elsif userinput1 > userinput2
>  else userinput1 == userinput2
> 
> So my question is how do I go about comparing my two arrays to determine
> a outcome? I think a hash is the way to go, not sure how to make the
> jump. please include code. Thanks,
> -- 
> Posted via http://www.ruby-forum.com/.

Dear Matt,

you can use the Array#index method and comparisons of these values:

a=["rock","paper","scissors"]
p a.index("rock")
p a.index("paper")
p a.index("scissors")
p a.index("rock")>a.index("paper")
p a.index("paper")>a.index("rock")
user_input1="rock"
user_input2="scissors"
p a.index(user_input1)>a.index(user_input2)

If the user enters something that isn't in your Array a, you'll get an error:
 
user_input2="ciseaux"
p a.index(user_input1)>a.index(user_input2)

You can catch that using a begin - rescue  clause ...

Best regards,

Axel 
-- 
Neu: GMX DSL bis 50.000 kBit/s und 200,- Euro Startguthaben!
http://portal.gmx.net/de/go/dsl02