[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: how to see if a variable matches an array element?

James Gray

8/9/2006 4:49:00 PM

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

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

die "Please choose one of #{choice.join(',')}." unless
choices.include? userchoice

Hope that helps.

James Edward Gray II


1 Answer

James Gray

8/9/2006 4:52:00 PM

0

On Aug 9, 2006, at 11:49 AM, James Edward Gray 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 Gray II