Created
December 14, 2018 07:31
-
-
Save judofyr/c192183735015a336fa0958894e22147 to your computer and use it in GitHub Desktop.
Tests and code in same file
This file contains 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
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 |
This file contains 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
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