top | dibdib: Random things | programming: Ruby  /  EDF  /  Palm  /  Perl | obsessions: Games | about: me  /  my pages  /  people

ruby programming

[download this file]

Combinations

Implements Array#combinations(n) which returns or yields (depending on block_given?) all the combinations of n elements from the array.

> ruby
require "combo"
a=[1,2,3,4,5]
p a.combinations(3)
^D
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
This was written for my script that calculates winnings from multi-way bets.