Ka Mok Ka Mok - 1 year ago 83
Ruby Question

How to get the permutation of an array without changing the order of its values?

For this array:

a = [8, 2, 22, 97, 38, 15]
, how can I get all sub-arrays of length three, in the fashion of a sliding window, without disturbing the order of the values.?

For example, the result should be:

Answer Source

You could pass a parameter for like this to achieve


This returns an Enumeration which you may iterate over. To turn the Enumeration into an array, call the to_a method:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download