[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?

Matthew Smillie

8/9/2006 4:51:00 PM

On Aug 9, 2006, at 17:35, John Fink 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.

Check the documentation for the Array class at http://ruby-do...

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.

2 Answers

Rick Rothstein \(MVP - VB\)

9/19/2008 12:56:00 AM

0

> My mistake, I forgot to mention that I need a cell formula,
> not a vba solution.

Since this is a programming newsgroup, it will normally provide VBA
solutions. If you are seeking formula solutions in the future, the
microsoft.public.excel.worksheet.functions would be a better newsgroup to
post to for them.

--
Rick (MVP - Excel)

Minitman

9/19/2008 5:20:00 AM

0

Noted.

-miniman

On Thu, 18 Sep 2008 20:55:35 -0400, "Rick Rothstein"
<rick.newsNO.SPAM@NO.SPAMverizon.net> wrote:

>> My mistake, I forgot to mention that I need a cell formula,
>> not a vba solution.
>
>Since this is a programming newsgroup, it will normally provide VBA
>solutions. If you are seeking formula solutions in the future, the
>microsoft.public.excel.worksheet.functions would be a better newsgroup to
>post to for them.