I'm porting a JavaScript library to Ruby, and have come across the following insanity (heavily abbreviated):
function foo(){
if (foo) ...
loop:
while(go()){
if (...) break;
switch(...){
case a:
break loop;
case b:
case c:
if (...) break loop;
...
break;
case d:
if (...) break loop;
// fall through
case e:
if (...) break loop;
...
break;
case f:
if (...) break loop;
object_init:
do{
switch(...){
case a:
...
break;
case b:
...
break object_init;
}
} while(...);
...
break;
}
}
}
(You can view the full horror on lines 701-1006.)
How would you rewrite this in Ruby? Specifically:
- Handling the intermixed
break
andbreak loop
, and - Handling the occasional "fall throughs" that occur in the switch
Presumably a good general strategy for these will get me through other situations, like the nested object_init
breaking that also occurs.
Edit: How silly of me; a JavaScript "fall through" like this:
switch(xxx){
case a:
aaa;
case b:
bbb;
break;
}
can easily be rewritten in Ruby as:
case xxx
when a, b
if a===xxx
aaa
end
bbb
end