From: Ryan Leavengood Date: 2006-05-24T03:06:46+09:00 Subject: Re: How to "go to" in a RUBY script . . . On 5/23/06, Simen wrote: > > There's also continuations. They're not only for web frameworks, you > know. It's fairly easy to write something goto-like with continuations. > I once did, for fun. You could do stuff like > > label :loop > [code] > goto :loop > > Easy to abuse, fun to write. Still, catch/throw should be sufficient > most of the time. This did sound fun, here is my quick and dirty implementation: $labels = {} def label(s) callcc do |cont| $labels[s] = cont end end def goto(s) cont = $labels[s] cont.call if cont end def method1 puts "In method1, goto :start" goto :start end if $0 == __FILE__ go = true label :start puts "Starting..." if go puts "Running loop..." i = 0 label :loop puts "i is #{i}" i += 1 goto :loop if i < 5 go = false method1 else puts "Not running loop." end end __END__ I'm definitely using this in the next obfuscated Ruby contest. In other words, this should not be used in real code. Ryan