Re: Compound conditionals in case when statements? Syntax?




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

On 10/17/07, Randy Kramer <rhkramer@xxxxxxxxx> 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... ;)

.



Relevant Pages

  • Re: Nuvi 750 nebie questions
    ... Hey, any help here? ... really miss the remote control. ... database still puts my home on the wrong side of a main street. ...
    (sci.geo.satellite-nav)
  • Re: OT: Thunderbird 0.4 "feels" better
    ... Hey, you're right. ... `simple' "Reply" puts *only* the list as recipient. ... To UNSUBSCRIBE, email to debian-user-request@lists.debian.org ...
    (Debian-User)
  • Re: Bamaham
    ... puts on a good show to. ... "Whats not to like?" ... Hey they're looking out for all the cute fuzzy animals;) ...
    (rec.motorcycles.racing)