Compound conditionals in case when statements? Syntax?

I need (or want :wink: to do something like the following:

when ((/^—++ (.*)/) and (new_record == true))

I’ve tried a lot of variations, but either this just won’t work, or I
haven’t
managed to guess the proper syntax. (I’ve also tried googling and
searching
in the pickaxe(2).)

Aside: Maybe the compound won’t work because one is what the pickaxe
calls a
condition and the other is a comparison? But, I’m not sure which is
which.

If this can’t work, I’ll try (I’ve been trying) a nested if statement,
but
I’ll ask about that in my next post. :wink:

Randy K.

On 10/17/07, Randy K. [email protected] wrote:

If this can’t work, I’ll try (I’ve been trying) a nested if statement, but
I’ll ask about that in my next post. :wink:

this can’t work for the simple reason that the objects given at ‘when’
are being sent the === message with the object in ‘case’. In your
‘when’ it’s the result of the stuff in closures.
This generally is the job of ‘if’, just keep in mind that case/when is
for matching, if/else is for conditionals.
One roundabout way exists to make this work though:

class Proc
def ===(obj)
self[obj]
end
end

case ‘foo’
when lambda{|e| new_record and /^—++ (.*)/ === e }
puts :aye_match
else
puts :sorry_you_lose
end

This is generally only advised if your middle name is some form of
(Advent|Dang)erous, I won’t be able to take responsibility for all
resulting damage. But also note that new_record being true or false
makes it simple to work with it without first comparing true/false
with true, the result won’t change, true cannot become any more true.

a = true

true

puts “hey” if a == true
hey

nil

puts “hey” if a
hey

Hope this helps a little bit. Have fun with ruby!

^ manveru

On Wed, 17 Oct 2007 07:39:48 +0900, Randy K. [email protected]
wrote:

I need (or want :wink: to do something like the following:

when ((/^—++ (.*)/) and (new_record == true))

When you see a case clause like this:

case obj
when foo

a

when bar

b

else

c

end

Picture it like this:

if foo === obj

a

elsif bar === obj

b

else

c

end

(the order of arguments to === is important)

So, when you write:

when ((/^—++ (.*)/) and (new_record == true))

It’s really like writing:

if ((/^—++ (.*)/) and (new_record == true)) === obj

… which isn’t quite what you want.

Using a nested if statement might be the simplest solution.

Incidentally, it isn’t usually necessary to write:

new_record == true

If new_record is true, the result will be true anyway, and
if it is false, the result will be false anyway. You can
just use:

new_record

…instead. Similarly, instead of either:

new_record != true
new_record == false

…you can simply write one of:

!new_record
not new_record

(! and not mean the same thing)

-mental

From: Randy K. [mailto:[email protected]]

Subject: Compound conditionals in case when statements? Syntax?

I need (or want :wink: to do something like the following:

when ((/^—++ (.*)/) and (new_record == true))

cases come in two forms,

1 classic switch form, like

case sw
when foo
#…
when bar
#…
else
#…
end

this form is interpreted using if is like (as already explained by
mentalguy),

if foo===sw
#…
elsif bar===sw
#…
else
#…
end

2 conditional form (similar to nested ifs), like

case
when foo
#…
when bar and condition1
#…
when condition2 or condition3
#…
else
#…
end

this form is interpreted using if like,

if foo
#…
elsif bar and condition1
#…
elsif condition2 or condition3
#…
else
#…
end

I would reckon fr your post that you may want the second form. So
something like,

case
when ((/^—++ (.*)/) and new_record
#…
end

But i’m not totally sure since you didn’t post any sample code. but
hey, if your code does not fit the cases, then by all means, do the
ifs…

hth.

kind regards -botp

On 17.10.2007 01:37, Michael F. wrote:

condition and the other is a comparison? But, I’m not sure which is which.

If this can’t work, I’ll try (I’ve been trying) a nested if statement, but
I’ll ask about that in my next post. :wink:

this can’t work for the simple reason that the objects given at ‘when’
are being sent the === message with the object in ‘case’. In your
‘when’ it’s the result of the stuff in closures.
This generally is the job of ‘if’, just keep in mind that case/when is
for matching, if/else is for conditionals.

See Pena’s comment: there is another form of ‘case’ which can be nicely
used to solve this.

Kind regards

robert

On Oct 16, 2007, at 6:37 PM, Michael F. wrote:

are being sent the === message with the object in ‘case’. In your

makes it simple to work with it without first comparing true/false
Hope this helps a little bit. Have fun with ruby!

^ manveru

Adventerous? That’s a strange middle name… :wink:

On 17.10.2007 03:46, Peña wrote:

case sw
if foo===sw
case
this form is interpreted using if like,

I would reckon fr your post that you may want the second form. So something like,

case
when ((/^—++ (.*)/) and new_record
#…
end

Maybe rather:

case
when new_record && /^—++ / =~ s

Note the matching operator, otherwise $_ will be matched. Note also,
that it is superfluous to match the rest of the string unless the
content need to be further processed. I’d probably also reverse the
order since I assume new_record is a local variable and that test is
likely faster than the regexp matching.

Kind regards

robert

I use:
case true
when a == b && c == d
when a != b && c == d

end

From: Robert K. [mailto:[email protected]]

case

when new_record && /^—++ / =~ s

oops, yes. That’s what i get on pasting without thinkig (nor testing :slight_smile:

Note the matching operator, otherwise $_ will be matched.

Note also, that it is superfluous to match the rest of the

string unless the content need to be further processed.

I’d probably also reverse the order since I assume

new_record is a local variable and that test is

likely faster than the regexp matching.

indeed and many thanks.
kind regards -botp

On Wednesday 17 October 2007 03:17 am, Peña, Botp wrote:

From: Robert K. [mailto:[email protected]]

case

when new_record && /^—++ / =~ s

Thanks to all who replied!

I’ve tried the suggested approaches and variations that I could think
of, and
still no luck.

I’ve attached the following small files in case anyone wants to try to
see if
they can get it to work:

  • askconvert: the code–at the point in question you will find two
    commented out lines and a comment that will be fairly self explanatory
    (I
    think)
  • test.txt: a test file
  • test.aml.txt: a file containing the desired result
  • utest: a bash script for doing a sort of poor man’s unit test–this
    may
    not work for you unless you make some modifications (like to the
    ./askconvert
    line)

The unit test works by running the program and then doing a diff between
text.aml (the output from the program) and test.aml.txt. Note that the
current date and time are incorporated in two of the header lines, the
From
and the Date: line, so those will always show up as different. So, I
visually inspect the diff for these two criteria:

  • the only lines in the diff related to From and Date lines with
    different
    dates
  • all titles in the diff should show up as variations of “Primary”

The other thing that will show up is the minor spacing issue mentioned
below.

Someday, I might write code (in that unit test file) to, one way or
another,
ignore those dates so they don’t show up in the diff. (I might process
test.aml and test.aml.std to replace the real date/times with dummys
that are
all the same.)

Note that there is one minor linespace issue remaining in the program
that I
just haven’t been able to resolve so far. It is so minor that I’m going
to
ignore it for now, but, you can spot it if you watch carefully when you
run
the diff.

Randy K.

On Wednesday 17 October 2007 01:46 pm, Randy K. wrote:

On Wednesday 17 October 2007 01:25 pm, Randy K. wrote:

I’ve tried the suggested approaches and variations that I could think of,
and still no luck.

Ok, for the record I got it to work–here’s a little working test
program:

#! /usr/bin/env ruby

new_record = true
line = “—++ tasty”

case
when (new_record and /^—++ (.)/ =~ line)
puts "a title if at the beginning of a record: " + $1
when /^—++ (.
)/ =~ line
puts "a level 2 heading if not at the beginning of a record: " + $1
else
puts “neither a title nor a level 2 heading”
end

My mistake was leaving “line” after “case” when I tried to switch to the
alternate form of case statement. Sometimes (or more often :wink: I can be
pretty dense.

(And, of course, the other thing I finally did was make a small test
snippet
of the code for experimentation.)

Randy K.

Oops, I had a dumb mistake in one of those two commented out lines in
askconvert. Fixing them doesn’t solve the problem, but if you don’t fix
them
you won’t get the program to work.

Attached is a revised copy of askconvert.

Sorry about that!
Randy K.

On Wednesday 17 October 2007 01:25 pm, Randy K. wrote:

I’ve tried the suggested approaches and variations that I could think of,
and