On Thu, 12 Jun 2003 12:28:06 +0900
Daniel Carrera <dcarrera / math.umd.edu> wrote:

> Here are two examples.  The point of them is not that it's hard to "do" in 
> Perl, but rather, that the Ruby solution has a very natural syntax.
> 
> Example 1:
> ==========
> 
> Task:  Compute factorials, combinations, permutations and binomial
> - -----  distributions.
> 
> Code:
> - -----
> 
> # Factorial.
> class Fixnum
>     def _! 
>         self <  0 and raise "Factorial only defined for non-negative
>         numbers" self == 0 and return 1
>         self * (self - 1)._!
>     end
> end

Hmmm.... I don't really like calling it "_!". There's no clean way to make it
look like math notation, so I wouldn't try. Lets call it "factorial". We can
claim we did for smalltalk compatibility. :-) (Smalltalk's numbers have a
"factorial" method.)

Also, I would probably implement it thus:

class Integer
  def factorial
    (1..self).inject { |f, n| f * n }
  end
end

.....just in case we want to calculate the factorials of numbers larger than
2**30 - 1 :-) Actually, to quote the pickaxe, "A Fixnum holds Integer values
that can be represented in a native machine word (minus 1 bit)." And another
bit goes to sign, (I think so, anyway) off by one due to starting at zero, and
you've got the upper limit as 2**30 - 1. (On 32 bits machines)

Jason Creighton