# How to see if a variable matches an array element?

**URL:** <https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522>\
**Category:** Ruby\
**Created:** [August 9, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522 "2006-08-09T19:26:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 9, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/1 "2006-08-09T19:26:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 9, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/2 "2006-08-09T21:26:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/3 "2006-08-15T14:11:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/4 "2006-08-15T14:22:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/5 "2006-08-15T14:22:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/6 "2006-08-15T14:22:11Z")

</div>

Yeehaw, that did it. Thanks David.

jf

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/7 "2006-08-15T14:22:11Z")

</div>

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 [RDoc Documentation](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.

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/8 "2006-08-15T14:22:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [August 15, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-see-if-a-variable-matches-an-array-element/64522/9 "2006-08-15T14:22:11Z")

</div>

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

jf
