Sha256: 3cb3f3c8ce5967a214768bbeec02805e6a40237285e1809c09a006516246730a

Contents?: true

Size: 886 Bytes

Versions: 1

Compression:

Stored size: 886 Bytes

Contents

module Polyfill
  module V2_4
    module Integer
      module Instance
        module Digits
          module Method
            def digits(base = 10)
              base = base.to_int
              raise Math::DomainError, 'out of domain' if self < 0
              raise ArgumentError, 'negative radix' if base < 0
              raise ArgumentError, "invalid radix #{base}" if base < 2

              acc = []
              remainder = self
              while remainder > 0
                remainder, value = remainder.divmod(base)
                acc.push(value)
              end
              acc
            end
          end

          if RUBY_VERSION < '2.4.0'
            refine ::Integer do
              include Method
            end

            def self.included(base)
              base.include Method
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polyfill-0.3.0 lib/polyfill/v2_4/integer/instance/digits.rb