Skip to content

Instantly share code, notes, and snippets.

@pjlowry
Created January 29, 2013 23:52
Show Gist options
  • Save pjlowry/4669170 to your computer and use it in GitHub Desktop.
Save pjlowry/4669170 to your computer and use it in GitHub Desktop.
Integer'ed in your face...
class Integer
def fibonacci
if self.kind_of? Integer
if self < 0
"Must be a positive number"
else
first_num = 0
second_num = 1
return self if (0..1).include? self
(1..self).each do |counter|
first_num, second_num = second_num, first_num + second_num
end
first_num
end
else
"Must be an integer"
end
end
end
puts "'#{8.fibonacci}' should equal '21'"
puts "'#{1.fibonacci}' should be '1'"
puts "'#{2.fibonacci}' should be '1'"
puts "'#{0.fibonacci}' should be '0'"
puts "'#{5.fibonacci}' should equal '5'"
puts "'#{6.fibonacci}' should equal '8'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment