From: Robert Dober Date: 2008-04-20T03:32:26+09:00 Subject: Re: How to "break" a "case-when" ? On Sat, Apr 19, 2008 at 7:59 PM, Christopher Dicely wrote: > On Fri, Apr 18, 2008 at 1:40 PM, Robert Dober wrote: > > > > On Fri, Apr 18, 2008 at 8:52 PM, Iñaki Baz Castillo wrote: > > > El Viernes, 18 de Abril de 2008, Peña, Botp escribió: > > > > > > > > > > how about another case way, > > > > > > > > var = ...something... > > > > case > > > > when num==1 and ( var < 0 || var > 10 ) > > > > puts "var not valid for num==1 !!!!!" > > > > when other_stuff_condition > > > > when .... > > > > ... > > > > else > > > > else_stuff > > > > end > > > > puts "Write it" > > > > > > opss, I didn't know that a "when" can contain a comparision, nice to know! > > It can contain any expression, but it will not do what you want. > > Sure it will. There are two different forms of case, the form First my appologies for my error, I did not see that the case part was empty, my fault > case > when foo1 then bar1 > when foo2 then bar2 > ... > else baz > end > > Works exactly as described above. > > You could do it with the other kind of case (the one whose semantics > you describe), > but it would be a bit uglier. E.g., > > case num > when (if var.between? 0, 10 then 1 else Object.new) then do_normal_stuff > when 1 then do_error_stuff > ... > end > > my point is however that of course you lose the basic feature you wanted but we have two possibilities here, either monkeypatching TrueClass and using the !! form to convert any logical true value to the real single true value, than the code might look like this case expression when /abc/ ... when !! get_an_object(expression) ... I have to admit that the monkey patch is just not an option if you are writing a library and is a feature too often abused anyway. I therefore suggest to follow Christopher's approach even if the code gets a little bit uglier (but beauty lies in the eyes of the beholder). case when /abc/ === expression ... when get_an_object( expression ) ... end Sorry again, I outsmarted myself ;) Cheers Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein