Breaking from 'case'

Hi,
is there any possibility to ‘break’ from ‘case’ ?

while run
case x
when ‘b’
``skip’’ if cond # how ???
something_else
else
something
end
timer = Time.now
end

Is there any possibility (in the example above) to skip the rest
of “when ‘b’” and continue at the line “timer = Time.now” using some
keyword/statement?

There’s always if-else-end option, but it seems to be inelegant in this
case due to indent-level increasing (I have three ifs in the real code)

Thanks in advance,
W.

···


Wejn <lists+rubytalk(at)box.cz>
(svamberk.net’s Linux section, fi.muni.cz student, linuxfan)

    Bored?  Want hours of entertainment?         <<<
      Just set the initdefault to 6!             <<<

My first impulse would be to wrap the case in a method and skip by
calling return, ie.:

def munge(x)
case x
when ‘b’
return
something_else
else
something
end
end

while run
munge x
timer = Time.now
end

But that tends not to refactor nicely: if you take code that
implements its control structure with return and factor some of it out
into a new method, then that new method may return to the wrong place.
If that is a fatal objection, or if you want to do it all inline, my
next impulse would be to use catch/throw, ie:

while run
catch :skip do
case x
when ‘b’
throw :skip if cond # how ???, like this !!!
something_else
else
something
end
end
timer = Time.now
end

HTH,

Jeremy Henty

···

In article 20020905005626.A15456@profa.box.cz, Wejn wrote:

while run
case x
when ‘b’
``skip’’ if cond # how ???
something_else
else
something
end
timer = Time.now
end

Is there any possibility (in the example above) to skip the rest
of “when ‘b’” and continue at the line “timer = Time.now” using some
keyword/statement?

Looks like you are making a case (no pun intended … ) for goto. :wink:

“Wejn” lists+rubytalk@box.cz wrote in message
news:20020905005626.A15456@profa.box.cz…

···

Hi,
is there any possibility to ‘break’ from ‘case’ ?

while run
case x
when ‘b’
``skip’’ if cond # how ???
something_else
else
something
end
timer = Time.now
end

Is there any possibility (in the example above) to skip the rest
of “when ‘b’” and continue at the line “timer = Time.now” using some
keyword/statement?

There’s always if-else-end option, but it seems to be inelegant in this
case due to indent-level increasing (I have three ifs in the real code)

Thanks in advance,
W.

          Wejn <lists+rubytalk(at)box.cz>

(svamberk.net’s Linux section, fi.muni.cz student, linuxfan)

    Bored?  Want hours of entertainment?         <<<
      Just set the initdefault to 6!             <<<

Try this:

catch( :breakout ) do
case x
when ‘b’
throw :breakout if cond
end
puts “This line skipped on breakout”
end
timer = Time.now

···

On Thu, Sep 05, 2002 at 07:51:20AM +0900, Wejn wrote:

Hi,
is there any possibility to ‘break’ from ‘case’ ?

while run
case x
when ‘b’
``skip’’ if cond # how ???
something_else
else
something
end
timer = Time.now
end


Alan Chen
Digikata LLC
http://digikata.com

My first impulse would be to wrap the case in a method and skip by
calling return, ie.:

[…snip…]

But that tends not to refactor nicely: if you take code that
implements its control structure with return and factor some of it out
into a new method, then that new method may return to the wrong place.
If that is a fatal objection, or if you want to do it all inline, my
next impulse would be to use catch/throw, ie:

[…snip…]

Thanks a lot! I did it with function call already …
but the second way seems to be a lot better.

W.

···


Wejn <lists+rubytalk(at)box.cz>
(svamberk.net’s Linux section, fi.muni.cz student, linuxfan)

    Bored?  Want hours of entertainment?         <<<
      Just set the initdefault to 6!             <<<

Or for extending the semantics of break. :slight_smile:

···

On Thu, Sep 05, 2002 at 09:05:51AM +0900, Shashank Date wrote:

Looks like you are making a case (no pun intended … ) for goto. :wink:


Alan Chen
Digikata LLC
http://digikata.com