|| and RegEx

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

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

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

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

“a” == aidy [email protected] 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