Luke Melia


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
> 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 created 1999. ··· Luke Melia created 1976. ··· Live With Passion!