Module: Radix

Defined in:
lib/radix.rb,
lib/radix/base.rb,
lib/radix/float.rb,
lib/radix/integer.rb,
lib/radix/numeric.rb,
lib/radix/rational.rb

Defined Under Namespace

Modules: BASE Classes: Base, Float, Integer, Numeric, Rational

Constant Summary collapse

DOT =

Radix separator used in string and array representations.

'.'
DIV =

Division character for rational numbers

'/'
DIVIDER =

Default seperator character.

" "

Class Method Summary collapse

Class Method Details

.const_missing(name) ⇒ String

Gets value of name in metadata or goes up ancestry.

Parameters:

  • name (Symbol)

Returns:



26
27
28
29
# File 'lib/radix.rb', line 26

def self.const_missing(name)
  key = name.to_s.downcase
  .key?(key) ? [key] : super(name)
end

.convert(number, from_base, to_base) ⇒ String

Convert a number of from_base as to_base.

Parameters:

Returns:

  • (String)

    The value of ‘digits` in `from_base` converted into `to_base`.



211
212
213
214
215
# File 'lib/radix/base.rb', line 211

def self.convert(number, from_base, to_base)
  from_base = Radix::Base.new(from_base) unless Radix::Base === from_base
  to_base   = Radix::Base.new(to_base)   unless Radix::Base === to_base
  to_base.convert(number, from_base)
end

.convert_base(digits, from_base, to_base) ⇒ String

Convert any base to any other base, using array of Fixnum’s. Indexes of the array correspond to values for each column of the number in from_base

Parameters:

  • digits (Array<Fixnum>)

    Array of values for each digit of source base.

  • from_base (Fixnum)

    Source Base

  • to_base (Fixnum)

    Destination Base

Returns:

  • (String)

    The value of digits in from_base converted as to_base.



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/radix/base.rb', line 231

def self.convert_base(digits, from_base, to_base)
  bignum = 0
  digits.each { |digit| bignum = bignum * from_base + digit }
  converted = []
  until bignum.zero?
    bignum, digit = bignum.divmod(to_base)
    converted.push(digit)
  end
  converted << 0 if converted.empty?  # THINK: correct?
  converted.reverse
end

.metadataHash{String=>String}

Returns the metadata contained in Radix.yml

Returns:



13
14
15
16
17
18
# File 'lib/radix.rb', line 13

def self.
  @metadata ||= (
    require 'yaml'
    YAML.load(File.new(File.dirname(__FILE__) + '/radix.yml'))
  )
end