I\'m going crazy: Where is the Ruby function for factorial? No, I don\'t need tutorial implementations, I just want the function from the library. It\'s not in Math!
Why would the standard library require a factorial method, when there is a built-in iterator for this exact purpose? It is called upto.
No you do not need to use recursion, like all these other answers show.
def fact(n)
n == 0 ? 1 : n * fact(n - 1)
end
Rather, the built-in iterator upto can be used to calculate factorials:
factorial = 1
1.upto(10) {|x| factorial *= x }
factorial
=> 3628800