teeworlds_network/lib/array.rb

29 lines
581 B
Ruby
Raw Normal View History

2022-11-05 16:48:47 +00:00
# frozen_string_literal: true
# I JUST REALIZED I ALREADY USED .scan(/../)
# TO GET .groups_of(2)
# AND DUUUUH .scan() IS BASICALLY ALREADY
# .groups_of()
# TODO: get rid of it?!
2022-11-16 10:30:13 +00:00
# update: no
# .scan(/../) works on strings and groups_of works on arrays
# so it has its purpose
class Array
def groups_of(max_size)
return [] if max_size < 1
groups = []
group = []
2022-11-05 16:19:05 +00:00
each do |item|
group.push(item)
if group.size >= max_size
groups.push(group)
group = []
end
end
2024-01-20 14:37:18 +00:00
groups.push(group) unless group.empty?
groups
end
end