Skip to content

Instantly share code, notes, and snippets.

@Cspeisman
Last active August 29, 2015 14:01
Show Gist options
  • Save Cspeisman/04c7d5d1ebaf96c3fe26 to your computer and use it in GitHub Desktop.
Save Cspeisman/04c7d5d1ebaf96c3fe26 to your computer and use it in GitHub Desktop.
# This is a complete example of FizzBuzz
# using an assert method to test my code.
# Essentially, all the assert method does is
# throw an error if my expectation evaluates to false
# an even simpler way to test would be by just using puts.
# for example: puts fizzbuzz(5) == buzz will print out true to my terminal
# if something prints false that means its a failing case
def fizzbuzz(number)
response = ''
response += 'fizz' if number % 3 == 0
response += 'buzz' if number % 5 == 0
return response if !response.empty?
number
end
1.upto(100) do |num|
puts fizzbuzz(num)
end
def assert(truthy)
raise "There was an error" if !truthy
end
assert fizzbuzz(1) == 1
assert fizzbuzz(2) == 2
assert fizzbuzz(3) == 'fizz'
assert fizzbuzz(4) == 4
assert fizzbuzz(5) == 'buzz'
assert fizzbuzz(9) == 'fizz'
assert fizzbuzz(15) == 'fizzbuzz'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment