# Basic || condition question perhaps

**URL:** <https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850>\
**Category:** Rails\
**Created:** [January 12, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850 "2008-01-12T20:35:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [January 12, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850/1 "2008-01-12T20:35:44Z")

</div>

I can’t figure out what is going wrong.

In a helper I have this line:

elsif player.send(next\_period) == (‘none’ || ‘bench’ )  
html\_holder \<\< " O"

This works for “none” but doesn’t seem to work for “bench”. I have  
double checked to make sure the attribute is set to exactly ‘bench’ with  
no spaces or case differences.

Is the condition syntax correct? can I go:

if x == (‘none’ || ‘one’ || ‘two’ || ‘three’)

or should I be doing it differently? I suppose I could be using a  
regular expression but an even more desirable way would be to test a  
string against a simple array of string options. I’ve thumbed through  
my ruby books but couldn’t find the answer.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [January 12, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850/2 "2008-01-12T20:51:08Z")

</div>

2008/1/12, Tom N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> In a helper I have this line:
> 
> elsif player.send(next\_period) == (‘none’ || ‘bench’ )  
> html\_holder \<\< " O"

See Array#include?

%w(none bench).include?(player.send(next\_period))

> This works for “none” but doesn’t seem to work for “bench”. I have  
> double checked to make sure the attribute is set to exactly ‘bench’ with  
> no spaces or case differences.
> 
> Is the condition syntax correct? can I go:
> 
> if x == (‘none’ || ‘one’ || ‘two’ || ‘three’)
> 
> or should I be doing it differently?

%w(none one two three).include? x

– Jean-François.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [January 12, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850/3 "2008-01-12T21:22:44Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [January 12, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850/4 "2008-01-12T22:37:26Z")

</div>

On 1/12/08, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if [‘none’, ‘one’, ‘two’, ‘three’].include?(x)

Or since we’re talking all Strings here

## if x =~ /^(none|one|two|three)$/

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [January 13, 2008, 12:24am UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850/5 "2008-01-13T00:24:28Z")

</div>

On 12 Jan 2008, at 19:35, Tom N. wrote:

> with  
> no spaces or case differences.
> 
> Is the condition syntax correct? can I go:
> 
> if x == (‘none’ || ‘one’ || ‘two’ || ‘three’)

for reference, the reason this doesn’t work is that ruby first evaluates  
‘none’ || ‘one’ || ‘two’ || ‘three’  
which evaluates to ‘none’ (since ‘none’ is not nil, the || stops there.  
So the above is the same as if x == ‘none’

Fred

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [January 12, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/basic-condition-question-perhaps/125850/6 "2008-01-12T22:09:12Z")

</div>

On Jan 12, 2008, at 1:35 PM, Tom N. wrote:

> Is the condition syntax correct? can I go:
> 
> if x == (‘none’ || ‘one’ || ‘two’ || ‘three’)

I don’t know if this is the preferred way, but I’d do

if [‘none’, ‘one’, ‘two’, ‘three’].include?(x)

Peace,  
Phillip
