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):
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.
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):
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
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
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
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.