# || and RegEx

**URL:** <https://www.ruby-forum.com/t/and-regex/65018>\
**Category:** Ruby\
**Created:** [August 15, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/and-regex/65018 "2006-08-15T18:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 15, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/and-regex/65018/1 "2006-08-15T18:39:05Z")

</div>

Hi,

Here is a snippet of a case statement

read\_in\_test\_data.each { |x|  
line = x.chomp  
case line  
when /^Provinces:$/ || /^Town:$/  
task = :provinces  
else  
#whatever  
end

|| should evaluate to true if either operands are true.

When I ran through the debugger:

var line == “Provinces:” # the task is set  
var line == “Town:” # the task is not set

I change the expression to &&

when /^Provinces:$/ && /^Town:$/

var line == “Provinces:” # the task is not set  
var line == “Town:” # the task is set

Could anyone tell me what I am doing wrong?

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 15, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/and-regex/65018/2 "2006-08-15T18:54:07Z")

</div>

On Wednesday, August 16, 2006, at 1:35 AM, aidy wrote:

> ```
> #whatever
> 
> ```
> 
> when /^Provinces:$/ && /^Town:$/
> 
> var line == “Provinces:” # the task is not set  
> var line == “Town:” # the task is set
> 
> Could anyone tell me what I am doing wrong?
> 
> aidy

Why not do something like this…

when /^(Provinces|Town):$/

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 15, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/and-regex/65018/3 "2006-08-15T18:57:07Z")

</div>

> case line  
> when /^Provinces:$/, /^Town:$/  
> task = :provinces  
> else  
> #whatever  
> end

Put commas between the values you want to threequal against, not the ||  
operator.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 15, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/and-regex/65018/4 "2006-08-15T19:21:09Z")

</div>

> > > > > “a” == aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

a\> case line  
a\> when /^Provinces:$/ || /^Town:$/

When you write this, ruby first execute ||, something like

moulon% ruby -e ‘p /^Provinces:$/ || /^Town:$/’  
/^Provinces:$/  
moulon%

fatally the result is /^Provinces:$/, then it will make the test

a\> task = :provinces

a\> when /^Provinces:$/ && /^Town:$/

Same here, it first evaluate &&

moulon% ruby -e ‘p /^Provinces:$/ && /^Town:$/’  
/^Town:$/  
moulon%

the result is /^Town:$/, then it make the test

Guy Decoux
