How to Chunk an Array in Ruby

How to chunk an array in Ruby

Use each_slice:

require 'enumerator' # only needed in ruby 1.8.6 and before
userids.each_slice(100) do |a|
# do something with a
end

How to split (chunk) a Ruby array into parts of X elements?

Take a look at Enumerable#each_slice:

foo.each_slice(3).to_a
#=> [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10"]]

How do I split an array into smaller arrays bsaed on a condition?

One more way to skin a cat

def contains_vowel(v) 
v.count("aeiou") > 0
end
def split_by_substring_with_vowels(arr)
arr.chunk_while do |before,after|
!contains_vowel(before) & !contains_vowel(after)
end.to_a
end
split_by_substring_with_vowels(arr)
#=> [["a"], ["b", "g"], ["e"], ["f", "h"], ["i"]]

What it does:

  • passes each consecutive 2 elements
  • splits when either of them contain vowels

Example with your other Array

arr = ["1)", "dwr", "lyn,", "18,", "bbe"]
split_by_substring_with_vowels(arr)
#=> [["1)", "dwr", "lyn,", "18,"], ["bbe"]]

Further example: (if you want vowel containing elements in succession to stay in the same group)

def split_by_substring_with_vowels(arr)
arr.chunk_while do |before,after|
v_before,v_after = contains_vowel(before),contains_vowel(after)
(!v_before & !v_after) ^ (v_before & v_after)
end.to_a
end

arr = ["1)", "dwr", "lyn,", "18,", "bbe", "re", "rr", "aa", "ee"]
split_by_substring_with_vowels(arr)
#=> [["1)", "dwr", "lyn,", "18,"], ["bbe", "re"], ["rr"], ["aa", "ee"]]

This checks if before and after are both not vowels Or if they both are vowels

Chunk array by condition

your_array
.chunk { |ary| ary.first.match?("Page") }
.each_slice(2)
.map { |group| group.flat_map(&:last) }
#=> [
# [["Page_1", nil], ["ROW1", "one"], ["ROW2", "four"], ["ROW3", "seven"], ["End", nil]],
# [["Page_2", nil], ["ROW4", "Ten"], ["ROW5", "thirteen"], ["End", nil]],
# [["Page_4", nil], ["ROW6", "Sixteen"], ["ROW7", "Nineteen"], ["End", nil]]
# ]

Ruby, is there a built in method to split an array into two sub arrays at a specified index?

There is :)

arr1 = [2, 1, 2, 2, 2].take 2
arr2 = [2, 1, 2, 2, 2].drop 2

Chunk a Ruby array according to streaks within it

Since Ruby 1.9 there's a much simpler way to solve this problem:

original_array.chunk{|x| x <=> 0 }.map{|a,b| a * b.size }

Enumerable.chunk will group all consecutive elements of an array together by the output of a block:

>> original_array.chunk{|x| x <=> 0 }
=> [[1, [3]], [-1, [-4, -6]], [1, [1]], [-1, [-10, -5]], [1, [7]], [-1, [-8]], [1, [9]], [-1, [-3, -7]], [1, [8, 10, 4, 2, 5]], [-1, [-2]], [1, [6]], [-1, [-1, -9]]]

This is almost exactly what OP asks for, except the resulting groups need to be counted up to get the final streaks array.

Best way to split an array of strings in ruby?

You need map{...}, not map(...) for correct syntax in Ruby here:

array = ["string_apple", "string_banana", "string_orange"]

# Assign to a different array:
split_array = array.map{ |s| s.split(/_/) }

# Or modify the original array in place:
array.map!{ |s| s.split(/_/) }

# [["string", "apple"], ["string", "banana"], ["string", "orange"]]

ruby - split an array into sub arrays when a value changes and ignore/delete that value

Simplest and most readable way would probably be:

a.chunk {|x| x==1 || nil}.map(&:last)
#=> [[1, 1], [1], [1, 1, 1]]

If you were okay with using Ruby on Rails, you can use an even simpler solution:

a.split(0).reject(&:empty?)
#=> [[1, 1], [1], [1, 1, 1]]

How to Split Ruby Array of Hashes

It can be achieved by using drop and take:

def split_by_index(array, n)
[array.take(n), array.drop(n)]
end


Related Topics



Leave a reply



Submit