This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
|
Original Post: ary / 3
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded
|
|
Advertisement
|
Sometimes I wish Array.partition
could split into more than halves. Something like partition_by or commonality. Like an Array./
method!
class Array
def / len
inject([]) do |ary, x|
ary << [] if [*ary.last].nitems % len == 0
ary.last << x
ary
end
end
end
>> products = %w[cycles vents hoops willies moogs rifles pools fawns tridents]
>> products / 3
=> [["cycles", "vents", "hoops"], ["willies", "moogs", "rifles"],
["pools", "fawns", "tridents"]]
>> products / 2
=> [["cycles", "vents"], ["hoops", "willies"],
["moogs", "rifles"], ["pools", "fawns"], ["tridents"]]
The other alternative being a hopscotch with a flip-flop.
Read: ary / 3