[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Compound conditionals in case when statements? Syntax?

Randy Kramer

10/16/2007 10:40:00 PM

I need (or want ;-) 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. ;-)

Randy Kramer

11 Answers

Michael Fellinger

10/16/2007 11:37:00 PM

0

On 10/17/07, Randy Kramer <rhkramer@gmail.com> wrote:
> I need (or want ;-) 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. ;-)

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

MenTaLguY

10/16/2007 11:41:00 PM

0

On Wed, 17 Oct 2007 07:39:48 +0900, Randy Kramer <rhkramer@gmail.com> wrote:
> I need (or want ;-) 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


Peña, Botp

10/17/2007 1:46:00 AM

0

From: Randy Kramer [mailto:rhkramer@gmail.com]
# Subject: Compound conditionals in case when statements? Syntax?
#
# I need (or want ;-) 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

John Joyce

10/17/2007 3:14:00 AM

0


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

> On 10/17/07, Randy Kramer <rhkramer@gmail.com> wrote:
>> I need (or want ;-) 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. ;-)
>
> 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
>
Adventerous? That's a strange middle name... ;)

Robert Klemme

10/17/2007 6:01:00 AM

0

On 17.10.2007 01:37, Michael Fellinger wrote:
> On 10/17/07, Randy Kramer <rhkramer@gmail.com> wrote:
>> I need (or want ;-) 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. ;-)
>
> 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

Robert Klemme

10/17/2007 6:04:00 AM

0

On 17.10.2007 03:46, Peña wrote:
> From: Randy Kramer [mailto:rhkramer@gmail.com]
> # Subject: Compound conditionals in case when statements? Syntax?
> #
> # I need (or want ;-) 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

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

Peña, Botp

10/17/2007 7:17:00 AM

0

From: Robert Klemme [mailto:shortcutter@googlemail.com]
# case
# when new_record && /^---\+\+ / =~ s

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

# 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

kim.toms@gmail.com

10/17/2007 12:52:00 PM

0

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

On Oct 16, 6:39 pm, Randy Kramer <rhkra...@gmail.com> wrote:
> I need (or want ;-) 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. ;-)
>
> Randy Kramer


Randy Kramer

10/17/2007 5:28:00 PM

0


Content-Type: Multipart/Mixed;
boundary="Boundary-00=_jWkFH5Zrh6K2Hdc"
On Wednesday 17 October 2007 03:17 am, Peña, Botp wrote:
> From: Robert Klemme [mailto:shortcutter@googlemail.com]
> # 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 Kramer

> oops, yes. That's what i get on pasting without thinkig (nor testing :)
>
> # 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
>
>

Randy Kramer

10/17/2007 5:49:00 PM

0


Content-Type: Multipart/Mixed;
boundary="Boundary-00=_SqkFHVRI2sVWoto"
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 Kramer

On Wednesday 17 October 2007 01:25 pm, Randy Kramer wrote:
> I've tried the suggested approaches and variations that I could think of,
and
> still no luck.