Method: String#divide
- Defined in:
- lib/dicom/ruby_extensions.rb
#divide(parts) ⇒ Array<String>
Note:
The length of self must be a multiple of parts, or an exception will be raised.
Divides a string into a number of sub-strings of exactly equal length.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dicom/ruby_extensions.rb', line 56 def divide(parts) raise ArgumentError, "Expected an integer (Fixnum). Got #{parts.class}." unless parts.is_a?(Fixnum) raise ArgumentError, "Argument must be in the range <1 - self.length (#{self.length})>. Got #{parts}." if parts < 1 or parts > self.length raise ArgumentError, "Length of self (#{self.length}) must be a multiple of parts (#{parts})." unless (self.length/parts).to_f == self.length/parts.to_f if parts > 1 sub_strings = Array.new sub_length = self.length/parts parts.times { sub_strings << self.slice!(0..(sub_length-1)) } return sub_strings else return [self] end end |