Sha256: cfa6c72bcff37a432a980bf816ef662270adaa311c89cf3daa61220b9f7356cd

Contents?: true

Size: 683 Bytes

Versions: 1

Compression:

Stored size: 683 Bytes

Contents

require 'simplecheck'
require 'split_into/split_error'

module SplitInto
  class <<self
    include Simplecheck
  end

  def self.split(dividend, divisor)
    check(dividend, divisor, Integer, error_message: 'Dividend and divisor must be of type Integer')
    check(divisor >= 0, error_message: 'Divisor is less than zero')
    check(divisor <= dividend, error_message: 'Divisor is greater than the dividend')

    return [] if divisor.zero?

    parts = Array.new(divisor){ dividend.div(divisor) }
    dividend.modulo(divisor).times { |i| parts[i] += 1 } 
    parts.reverse
  rescue Simplecheck::CheckFailed => exception
    raise SplitInto::SplitError, exception.message
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
split_into-1.1 lib/split_into.rb