Class: String

Inherits:
Object show all
Defined in:
lib/shenanigans/string/in_groups_of.rb

Instance Method Summary collapse

Instance Method Details

#in_groups_of(size) ⇒ Object

Returns an array of the string broken down into groups of size characters.

"aabbcc".in_groups_of(2)
#=> ['aa', 'bb', 'cc']
"".in_groups_of(2)
#=> []
"".in_groups_of(0)
#=> ArgumentError

Raises:

  • (ArgumentError)


10
11
12
13
# File 'lib/shenanigans/string/in_groups_of.rb', line 10

def in_groups_of(size)
  raise ArgumentError, "Size of group must be >= 1" if size < 1
  scan(/.{1,#{size}}/)
end