Module: Radix

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

Defined Under Namespace

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

Constant Summary collapse

DOT =

Redix separator used in string and array representations.

'.'
DIV =
'/'
DIVIDER =
" "

Class Method Summary collapse

Class Method Details

.__DIR__Object



5
6
7
# File 'lib/radix/meta/data.rb', line 5

def self.__DIR__
  File.dirname(__FILE__)
end

.const_missing(name) ⇒ Object



23
24
25
26
# File 'lib/radix/meta/data.rb', line 23

def self.const_missing(name)
  key = name.to_s.downcase
  package[key] || profile[key] || super(name)
end

.convert(number, from_base, to_base) ⇒ Object

Convert number from it’s given base to antoher base. Do a standard conversion upto base 62.



107
108
109
110
111
# File 'lib/radix/base.rb', line 107

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) ⇒ Object

Convert any base to any other base, using array of digits.



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/radix/base.rb', line 114

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

.packageObject



9
10
11
12
13
14
# File 'lib/radix/meta/data.rb', line 9

def self.package
  @package ||= (
    require 'yaml'
    YAML.load(File.new(__DIR__ + '/package'))
  )
end

.profileObject



16
17
18
19
20
21
# File 'lib/radix/meta/data.rb', line 16

def self.profile
  @profile ||= (
    require 'yaml'
    YAML.load(File.new(__DIR__ + '/profile'))
  )
end