Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Last active December 11, 2015 09:58
Show Gist options
  • Save mfpiccolo/4583679 to your computer and use it in GitHub Desktop.
Save mfpiccolo/4583679 to your computer and use it in GitHub Desktop.
You can use the fibonacci method on positive integers an it will return the correlating fibonacci number.
class Integer
def fibonacci
if self < 0
"You can only use the fibonacci method with positive integers"
else
return self if (0..1).include? self
(self -1).fibonacci + (self - 2).fibonacci
end
end
end
puts "'#{8.fibonacci}' should be '21'"
puts "'#{0.fibonacci}' should be '0'"
puts "'#{1.fibonacci}' should be '1'"
puts "'#{2.fibonacci}' should be '1'"
puts "'#{3.fibonacci}' should be '2'"
puts "'#{4.fibonacci}' should be '3'"
puts "'#{5.fibonacci}' should be '5'"
puts "'#{-1.fibonacci}' should be 'You can only use the fibonacci method with positive integers'"
puts "'#{10.fibonacci}' should be '55'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment