[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

|| and RegEx

aidy

8/15/2006 4:35:00 PM

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

3 Answers

Stefan Lang

8/15/2006 4:52:00 PM

0


On Wednesday, August 16, 2006, at 1:35 AM, aidy wrote:
>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

--
Posted with http://De.... Sign up and save your mailbox.

Sam Smoot

8/15/2006 4:55:00 PM

0

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

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

ts

8/15/2006 5:21:00 PM

0

>>>>> "a" == aidy <aidy.rutter@gmail.com> 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