Skip to content

Instantly share code, notes, and snippets.

@MonicaDev
Last active August 29, 2015 13:56
Show Gist options
  • Save MonicaDev/9336796 to your computer and use it in GitHub Desktop.
Save MonicaDev/9336796 to your computer and use it in GitHub Desktop.
def assert
raise "Assertion failed!" unless yield
end
name = "bettysue"
assert { name == "bettysue" }
assert {name == "kary"} #this is the yield. This is true.
def is_fibonacci?(i)
fib_a = [1,1]
until fib_a[-1] >= i do
fib_a << (fib_a[-2] + fib_a[-1])
end
fib_a[-1] == i
raise "Assertion failed!" unless yield
end
#driver test code
is_fibonacci?(144){true}
is_fibonacci?(1596){false}
is_fibonacci?(610){true}
is_fibonacci?(5000){false}
is_fibonacci?(6600){true}
is_fibonacci?(6765){true}
#good_guess exercise
def good_guess?(i)
raise "Assertion failed!" unless yield
if i == 42
true
else
false
end
end
good_guess?(42){false}
good_guess?(42){true}
#------
#When I ran it from textmate and on the terminal command line- I got this
=begin
Monicas-MacBook-Air:programs monicacho$ ruby assert_statements-original_gist.rb
assert_statements-original_gist.rb:8:in `assert': Assertion failed! (RuntimeError)
from assert_statements-original_gist.rb:13:in `<main>'
Monicas-MacBook-Air:programs monicacho$
=end
#-------
#basically line #10 is saying that the last command is failed. Yelling "Assertion failed!" irb :6 is failed.
# Run your assert statements to find out which test has an incorrect expectation. The error shown should point you to a line number trace of the offending assert statement. Correct the expectation (`true` or `false`?), and rerun the code to make sure there are no other errors.
#
#Write down the integer that failed the assertion below:
# is_fibonacci?(1596){false}
#is_fibonacci?(5000){false}
#5) BONUS: INCLUDE A CUSTOM ERROR MESSAGE
#Your new assert method code and test code here:
def asserted
raise "Failed!" unless yield
end
name = "Maria"
asserted { name == "Maria"}
asserted { name == "Caroline"}
#6) Reflection
# this was cool.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment