How to see if a variable matches an array element?

Hey folks,

I’m knocking together a Rock, Paper, Scissors Ruby script to teach
myself
more Ruby deliciousness, and I’ve got it working, except I want to
compare a
variable to an array element (one of [“R”, “P”, “S”]) and have the
program
die if it doesn’t match up.

Here’s the relevant part of my script (note the rochambo function is
defined
earlier in the script, and is really verbose, so I clipped it out):

choices = %w{R P S}
puts “Your choice? R,P,S”
userchoice = gets.chomp.upcase
computerchoice = choices[rand(choices.length)]
puts "You picked " + userchoice + “!”
puts "Computer picked " + computerchoice + “!”
puts “FIGHT!”
rochambo(userchoice, computerchoice)

Basically I want to have the program die if the userchoice variable
doesn’t
equal one of the elements in the choices array, and it should be easy,
but I
am hitting my head against a wall.

jf

John F. wrote:

Hey folks,

I’m knocking together a Rock, Paper, Scissors Ruby script to teach
myself
more Ruby deliciousness, and I’ve got it working, except I want to
compare a
variable to an array element (one of [“R”, “P”, “S”]) and have the
program
die if it doesn’t match up.

Here’s the relevant part of my script (note the rochambo function is
defined
earlier in the script, and is really verbose, so I clipped it out):

choices = %w{R P S}
puts “Your choice? R,P,S”
userchoice = gets.chomp.upcase
computerchoice = choices[rand(choices.length)]
puts "You picked " + userchoice + “!”
puts "Computer picked " + computerchoice + “!”
puts “FIGHT!”
rochambo(userchoice, computerchoice)

Basically I want to have the program die if the userchoice variable
doesn’t
equal one of the elements in the choices array, and it should be easy,
but I
am hitting my head against a wall.

jf

irb(main):031:0> choices = %w{R P S}
=> [“R,P,S”]
irb(main):032:0> computerChoice = gets.chomp.upcase()
y
=> “Y”
irb(main):033:0> choices.include?(computerChoice)
=> false
irb(main):034:0> computerChoice = gets.chomp.upcase()
r
=> “R”
irb(main):035:0> choices.include?(computerChoice)
=> true

Hi –

On Thu, 10 Aug 2006, John F. wrote:

choices = %w{R P S}
puts “Your choice? R,P,S”
userchoice = gets.chomp.upcase

raise unless choices.include?(userchoice) # or something like that

David

On Aug 9, 2006, at 11:35 AM, John F. wrote:

puts “FIGHT!”
rochambo(userchoice, computerchoice)

Basically I want to have the program die if the userchoice variable
doesn’t
equal one of the elements in the choices array, and it should be
easy, but I
am hitting my head against a wall.

die “Please choose one of #{choice.join(’,’)}.” unless
choices.include? userchoice

Hope that helps.

James Edward G. II

John F. wrote:

defined

Basically I want to have the program die if the userchoice variable
doesn’t
equal one of the elements in the choices array, and it should be easy,
but I
am hitting my head against a wall.

jf

Something like:

unless choices.include? userchoice
puts “You must choose R, P, or S!”
exit
end

-Justin

Yeehaw, that did it. Thanks David.

jf

On Aug 9, 2006, at 17:35, John F. wrote:

Here’s the relevant part of my script (note the rochambo function
rochambo(userchoice, computerchoice)

Basically I want to have the program die if the userchoice variable
doesn’t
equal one of the elements in the choices array, and it should be
easy, but I
am hitting my head against a wall.

Check the documentation for the Array class at http://ruby-doc.org/core

exit unless choices.include? userchoice

will accomplish what you’re looking for, though there are quite a few
other ways to do it.

matthew smillie.

On Aug 9, 2006, at 11:49 AM, James Edward G. II wrote:

die “Please choose one of #{choice.join(’,’)}.” unless
choices.include? userchoice

I must be having Perl flashbacks today. die should be raise, of
course. I have no idea where that came from!

James Edward G. II

And that too. As always, more than one way to skin a cat. I had
totally
forgotten about unless somehow. Thanks everybody.

jf

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs