How to "break" out of a case...while in Ruby

omninonsense picture omninonsense · Nov 5, 2011 · Viewed 13.4k times · Source

So, I've tried break, next and return. They all give errors, exit of course works, but that completely exits. So, how would one end a case...when "too soon?"

Example:

case x
    when y; begin
        <code here>
        < ** terminate somehow ** > if something
        <more code>
    end
end

(The above is some form of pseudo-code just to give the general idea of what I'm asking [begin...end was used with the hope that break would work].

And, while I'm at it, is there a more elegant way of passing blocks to case...when?

Answer

Gareth picture Gareth · Nov 5, 2011

What's wrong with:

case x
when y;
    <code here>
    if !something
        <more code>
    end
end

Note that if !something is the same as unless something