Topic: Falling through case?

Is there any way to work with "case" (or something similar that I can't find) and have the actions fall through? (Think not writing "break" in most other languages switch statement.)

Re: Falling through case?

case actually does fall through.  It evaluates each when.  Can you post a better explanation of what you're trying to do?

BrewControl.com - Brewery and Brewpub management powered by RoR

Re: Falling through case?

Rather than

case my_variable
  when A
    do a
    do b
    do c
  when B
    do b
    do c
  when C
    do c
end

or
case my_variable
  when A
    do a
  when (A || B)
    do b
  when (A || B || C)
    do c
end

I'm used to having to go:
[code=C]switch (my_variable) {
  case A:
    do a;
    do b;
    do c;
    break;
  case B:
    do b;
    do c;
    break;
...
}[/code]
or
[code=C]switch (my_variable) {
  case A:
    do a;
  case B:
    do b;
...
}[/code]
So long as "case A" evaluated to true, until a "break" is found, all instructions will be executed.

Last edited by KittyKate (2007-04-24 15:22:02)

Re: Falling through case?

I'm familiar with the switch/case forms in several languages.  I was looking for more of the meat on what you're trying to do.  Sometimes the mind takes you down the wrong path, and you end up trying to drive a nail with a screwdriver.  The answer may lie somewhere else.

BrewControl.com - Brewery and Brewpub management powered by RoR

Re: Falling through case?

Basically, if A is true, do a, b, c, and d. If B is true, do b, c, and d. If c is true...

Re: Falling through case?

I was mistaken.  I misread the docs on exactly how the case statements work.  I don't see a way of using case to do what you want without repeating the statements in the when clauses.

BrewControl.com - Brewery and Brewpub management powered by RoR

Re: Falling through case?

That's what I thought. Which defeats one of the greatest asset of a switch statement. Of course, it also removes the problem of forgetting to put in "break"s, but it's such a break from convention (or at least with the other languages I've worked with)!