From: Michael Fellinger Date: 2007-10-17T08:37:08+09:00 Subject: Re: Compound conditionals in case when statements? Syntax? On 10/17/07, Randy Kramer 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