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"]]
Dividing elements of a ruby array into an exact number of (nearly) equal-sized sub-arrays
You're looking for Enumerable#each_slice
a = [0, 1, 2, 3, 4, 5, 6, 7]
a.each_slice(3) # => #<Enumerator: [0, 1, 2, 3, 4, 5, 6, 7]:each_slice(3)>
a.each_slice(3).to_a # => [[0, 1, 2], [3, 4, 5], [6, 7]]
Need to split arrays to sub arrays of specified size in Ruby
arr.each_slice(3).to_a
each_slice
returns an Enumerable, so if that's enough for you, you don't need to call to_a
.
In 1.8.6 you need to do:
require 'enumerator'
arr.enum_for(:each_slice, 3).to_a
If you just need to iterate, you can simply do:
arr.each_slice(3) do |x,y,z|
puts(x+y+z)
end
How to split an array into n equal or close to equal arrays?
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.group_by.with_index{|_, i| i % 2}.values
# => [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
a.group_by.with_index{|_, i| i % 3}.values
# => [[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]
a.group_by.with_index{|_, i| i % 4}.values
# => [[1, 5, 9], [2, 6, 10], [3, 7], [4, 8]]
a.group_by.with_index{|_, i| i % 5}.values
# => [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
a.group_by.with_index{|_, i| i % 6}.values
# => [[1, 7], [2, 8], [3, 9], [4, 10], [5], [6]]
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
Splitting array in three different arrays of maximum three elements
Try this...
Using Enumerable#each_slice
to slice array x value
array = [12, 13, 14, 18, 17, 19, 30, 23]
array.each_slice(3)
array.each_slice(3).to_a
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
Split an array in ruby based on some condition
Try the partition method
@arr1, @arr2 = @level1.partition { |x| x[0] > 2.1 }
The condition there may need to be adjusted, since that wasn't very well specified in the question, but that should provide a good starting point.
Split an array into some sub-arrays
You can use the #each_slice
method on the array
big_array = (0..20).to_a
array = big_array.each_slice(2).to_a
puts array # [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15], [16, 17], [18, 19], [20]]
Related Topics
Rails Activerecord Perform Group, Sum and Count in One Query
Why Is Division in Ruby Returning an Integer Instead of Decimal Value
Connecting Rails 3.1 With Multiple Databases
What's the Difference Between Uri.Escape and Cgi.Escape
Saving Enum from Select in Rails 4.1
How to Set Default Values in Rails
How to Convert a Bigdecimal to a 2-Decimal-Place String
The Authorization Mechanism You Have Provided Is Not Supported. Please Use Aws4-Hmac-Sha256
Is 'Eval' Supposed to Be Nasty
How to Find and Return a Duplicate Value in Array
What Exactly Is the Singleton Class in Ruby
How to Replace Accented Latin Characters in Ruby