Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping equivalent of Array#slice [duplicate]

Tags:

arrays

ruby

Given this Ruby array:

[1, 2, 3, 4, 5]

What is the easiest way to iterate it like this?

[[1,2], [2,3], [3,4], [4,5]]

Or this?

[[1,2,3], [2,3,4], [3,4,5]]
like image 986
Jostein Avatar asked Feb 28 '13 21:02

Jostein


People also ask

What is array overlapping?

Compares whether two arrays have at least one element in common. Returns TRUE if there is at least one element in common; otherwise returns FALSE. The function is NULL-safe, meaning it treats NULLs as known values for comparing equality.

How do you compare two arrays with snowflakes?

Usage Notes In Snowflake, arrays are multi-sets, not sets. In other words, arrays can contain multiple copies of the same value. ARRAY_INTERSECTION compares arrays by using multi-set semantics (sometimes called “bag semantics”), which means that the function can return multiple copies of the same value.

How do you make an array of snowflakes?

How to define an array variable in snowflake worksheet? set columns = (SELECT array_agg(COLUMN_NAME) FROM INFORMATION_SCHEMA. COLUMNS where table_name='MEMBERS');


1 Answers

each_cons (docs) does this. You just pass it the size of the chunks you want and it will yield them to the block you pass.

If you actually want the arrays, then you can of course chain this with to_a, for example

(1..5).each_cons(3).to_a
like image 138
Frederick Cheung Avatar answered Oct 12 '22 00:10

Frederick Cheung