Last active
May 10, 2021 23:01
-
-
Save cantremember/550208be0720eb24e02a706ae845c8aa to your computer and use it in GitHub Desktop.
Ruby's Kernel#catch and #throw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def catchAndThrow(flow) | |
i = 0 | |
if (flow == :return) then | |
return i | |
end | |
catch do |label| | |
# TODO: something useful | |
i += 1 | |
throw label if (flow == :throw) | |
# TODO: moar good stuffs | |
i += 1 | |
end | |
return i | |
end | |
require 'minitest/autorun' | |
describe 'catchAndThrow' do | |
it 'executes a return flow' do | |
_( catchAndThrow(:return) ).must_equal 0 | |
end | |
it 'executes a catch-and-throw flow' do | |
_( catchAndThrow(:throw) ).must_equal 1 | |
end | |
it 'executes any other flow' do | |
_( catchAndThrow(:other) ).must_equal 2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment