Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created December 14, 2018 07:31
Show Gist options
  • Save judofyr/c192183735015a336fa0958894e22147 to your computer and use it in GitHub Desktop.
Save judofyr/c192183735015a336fa0958894e22147 to your computer and use it in GitHub Desktop.
Tests and code in same file
minitest-same-file $ ruby fib.rb
minitest-same-file $ ruby -rminitest/autorun fib.rb
Run options: --seed 15401
# Running:
.
Finished in 0.000960s, 1041.6668 runs/s, 1041.6668 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
class Fib
def initialize
@cache = {}
end
def call(i)
@cache[i] ||= _call(i)
end
def _call(i)
if i <= 1
i
else
call(i-1) + call(i-2)
end
end
class FibTest < Minitest::Test
def test_fib
fib = Fib.new
assert_equal 8, fib.call(6)
end
end if defined?(Minitest)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment