Please Forward: Ruby Quiz Submission

From: Adam <skidooer@gmail.com>
Date: June 23, 2006 9:08:17 PM CDT
To: submission@rubyquiz.com
Subject: Please Forward: Ruby Quiz Submission

pp Pascal (#84)
-----------------------

class Fixnum
def factorial
   (1..self).to_a.inject(1) { |sum,i| sum *= i }
end

def choose(k)
   self.factorial / (k.factorial * (self - k).factorial)
end

def times_for_collection
   collection =
   times { |i| collection << yield(i) }
   collection
end
end

class Array
def to_triangle
   collect { |row| ' ' * ((last.size - row.size) / 2) + row + "\n" }.join
end
end

ARGV[0].to_i.times_for_collection do |n|
(n + 1).times_for_collection do |k|
   (k == 0 ? '%d' : '%6d') % (n > 1 ? (n - 1).choose(k - 1) + (n -
1).choose(k) : 1)
end.join
end.to_triangle.display

···

Begin forwarded message: