Sha256: 87cefc321aca7a34b70b7111ee779a4d6d759b65820ef379c36156c248b46524

Contents?: true

Size: 361 Bytes

Versions: 5

Compression:

Stored size: 361 Bytes

Contents

class String
  # Returns an array of the string broken down into groups of
  # <tt>size</tt> characters.
  #   "aabbcc".in_groups_of(2) #=> ['aa', 'bb', 'cc']
  #   "".in_groups_of(2) #=> []
  #   "".in_groups_of(0) #=> ArgumentError
  def in_groups_of(size)
    raise ArgumentError, "Size of group must be >= 1" if size < 1

    scan(/.{1,#{size}}/)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
shenanigans-1.0.18 lib/shenanigans/string/in_groups_of.rb
shenanigans-1.0.17 lib/shenanigans/string/in_groups_of.rb
shenanigans-1.0.16 lib/shenanigans/string/in_groups_of.rb
shenanigans-1.0.15 lib/shenanigans/string/in_groups_of.rb
shenanigans-1.0.14 lib/shenanigans/string/in_groups_of.rb