Skip to content

Instantly share code, notes, and snippets.

@cantremember
Last active May 10, 2021 23:01
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 cantremember/550208be0720eb24e02a706ae845c8aa to your computer and use it in GitHub Desktop.
Save cantremember/550208be0720eb24e02a706ae845c8aa to your computer and use it in GitHub Desktop.
Ruby's Kernel#catch and #throw
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