Skip to content

Instantly share code, notes, and snippets.

@lovemyliwu
Created June 13, 2020 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovemyliwu/0ee41e49aad2ae1066b8c89a4a000cda to your computer and use it in GitHub Desktop.
Save lovemyliwu/0ee41e49aad2ae1066b8c89a4a000cda to your computer and use it in GitHub Desktop.
ruby call/cc demo
require 'continuation'
def strange
callcc { |cont| return cont }
print "Back in method, "
end
print "Before method. "
c = strange()
print "After method. "
c.call if c
@lovemyliwu
Copy link
Author

lovemyliwu commented Jun 13, 2020

模拟break

require 'continuation'
def find_word(word, word_list)
    callcc { 
        |cont| 
        for item in word_list do
            puts item
            if (item == word)
                cont.call(item)
            end
        end
    }
end
puts find_word(2,[4,3,2,1])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment