Possible Duplicate:
ruby array element grouping
Example. Given array a:
a = [1, 2, 3]
Its length is 3 so I want to print all 2-length arrays. These are:
[1, 2]
[1, 3]
[2, 3]
I don’t know if there is some method in Ruby to get subset arrays. If there is not such a method what is most efficient way to do achieve this.
That’s just a simple combination of 2 elements:
[EDIT] As @Joshua pointed out in a comment, the docs state that the order is not guaranteed (!). So here is a functional implementation that generates the combinations in the order you asked for. For completeness, I’ll make it lazy as the original
combinationmethod: