Ruby reads your mind. You want cons ecutive elements?
[1, 2, 3, 4, 5, 6, 7, 8, 9].each_cons(2).to_a
# => [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
More Related Contents:
- how to i add objects to an array ? ruby
- How do I pick randomly from an array?
- How to check if a value exists in an array in Ruby
- How to select unique elements
- How to find and return a duplicate value in array
- Array#each vs. Array#map
- What does %w(array) mean?
- “for” vs “each” in Ruby
- How to split (chunk) a Ruby array into parts of X elements? [duplicate]
- How to find an item in array which has the most occurrences [duplicate]
- How to count duplicate elements in a Ruby array
- what’s different between each and collect method in Ruby [duplicate]
- Find indices of elements that match a given condition
- Creating matrix with `Array.new(n, Array.new)`
- Difference between map and collect in Ruby?
- Create two-dimensional arrays and access sub-arrays in Ruby
- Performance of Arrays and Hashes in Ruby
- Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays
- Ruby method Array#
- Need to split arrays to sub arrays of specified size in Ruby [duplicate]
- In Ruby, why does Array.new(size, object) create an array consisting of multiple references to the same object?
- Ruby – Difference between Array#
- Sort an array according to the elements of another array
- Convert array of 2-element arrays into a hash, where duplicate keys append additional values
- How to count duplicates in Ruby Arrays
- How to sort an array in Ruby to a particular order?
- How do you add an array to another array in Ruby and not end up with a multi-dimensional result?
- How to count identical string elements in a Ruby array
- Ruby: filter array by regex?
- Ruby array subtraction without removing items more than once