Calculating a factorial in Ruby : Fixnum extension « Number « Ruby






Calculating a factorial in Ruby


class Fixnum
  def factorial
    (1..self).inject { |a, b| a * b }
  end
end

puts 8.factorial

 








Related examples in the same category

1.Simulating a Subclass of Fixnum
2.NoMethodError: undefined method 'new' for Fixnum:Class
3.Add double_upto method to Fixnum
4.redefine basic arithmetic