Ka Mok Ka Mok - 8 months ago 42
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:


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: