|| and RegEx

A

aidy

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
 
K

Kevin Olbrich

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

Why not do something like this...

when /^(Provinces|Town):$/

_Kevin
www.sciwerks.com
 
S

Sam Smoot

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

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

ts

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top