Luke Melia

software dev

February 11, 2007

Cool ruby code

While sipping from the firehose that is the ruby-talk mailing list, I came across this sweet bit of code:

class Integer
  def factorial
    return 1 if self <= 1
    self * (self-1).factorial
  end
end
 
> 6.factorial
> 720

This is a nice illustration of ruby’s elegance. So much nicer than calling something like Math.Factorial(6).

Leave a Reply

LukeMelia.com created 1999. ··· Luke Melia created 1976. ··· Live With Passion!