I am porting the JavaScript library in Ruby and came across the following madness (greatly 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 complete horror at lines 701-1006 .)
How would you rewrite this in Ruby? In particular:
- Handling mixed
break and break loop and - Handling random dips that occur in the switch
Presumably a good general strategy for them will lead me to other situations, such as a nested object_init gap.
Edit : how stupid I am; JavaScript fails as follows:
switch(xxx){ case a: aaa; case b: bbb; break; }
can be easily rewritten in Ruby as:
case xxx when a, b if a===xxx aaa end bbb end
source share