Skip to content

Instantly share code, notes, and snippets.

@whitehat101
Created June 2, 2016 00:54
Show Gist options
  • Save whitehat101/5af53ca74fb93196c084228971232dfe to your computer and use it in GitHub Desktop.
Save whitehat101/5af53ca74fb93196c084228971232dfe to your computer and use it in GitHub Desktop.
Adds :fact to Fixnum
module WhiteHat101
module Factorial
# returns 1 for input < 1
def fact
(1..self).inject 1, :*
end
alias_method :!, :fact
end
end
Fixnum.send :include, WhiteHat101::Factorial
# 6.fact => 720
# 0.fact => 1
# 3.! => 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment