From: Brian Candler Date: 2007-03-01T05:13:23+09:00 Subject: Re: Ruby's "case" doesn't behave like a normal switch On Thu, Mar 01, 2007 at 04:36:39AM +0900, Pit Capitain wrote: > gga schrieb: > >For completeness sake, you can also emulate C's lack of a break; > >statement in ruby by doing redo/retry, like: > > > >a = 1 > > > >catch(:redo) do > > case a > > when 1 > > print '1' > > a = 2 > > redo > > when 2 > > puts 'and 2' > > end > >end > > > >This makes ruby's case statement probably the best of any language. > > I didn't know the "catch(:redo)". Is this documented somewhere? 'redo' is documented at http://www.rubycentral.com/book/tut_expressions.html 'catch' is documented at http://www.rubycentral.com/book/tut_exceptions.html However they're not a pair, and I don't think that 'catch' does anything in particular in the above example, apart from causing the block to be executed once. You could achieve the same with: a = 1 1.times do case a when 1 print '1' a = 2 redo when 2 puts 'and 2' end end Or: a = 1 begin case a when 1 print '1' a = 2 redo when 2 puts 'and 2' end end while false Regards, Brian.