Module: Radix

Defined in:
lib/radix/base.rb,
lib/radix/number.rb,
lib/radix/meta/data.rb

Overview

Radix coverts to and from any base.

Base conversions with ASCII ordered notations are easy in Ruby.

255.to_s(16)   #=> "FF"
"FF".to_i(16)  #=> 255

But Ruby reaches it’s limit at base 36.

255.to_s(37)   #=> Error

Radix provides the means of converting to and from any base.

Radix::Base.convert_base([100, 10], 256, 10)
#=> [2,5,6,1,0]

And it can handle any notation upto base 62.

Radix::Base.convert("10", 62, 10)  #=> "62"

And the notations need not be in ASCII order –odd notations can be used.

b10 = Radix::Base.new([:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U])
b10.convert("FF", 16) #=> "EYY"

Defined Under Namespace

Classes: Base, Number

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

Do a standard conversion upto base 62.



116
117
118
# File 'lib/radix/base.rb', line 116

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

.convert_base(digits, from_base, to_base) ⇒ Object

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



121
122
123
# File 'lib/radix/base.rb', line 121

def self.convert_base(digits, from_base, to_base)
  Radix::Base.convert_base(digits, from_base, to_base)
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

.standard_radix(integer_base) ⇒ Object

Provide a standard representation of a base upto 62.



126
127
128
# File 'lib/radix/base.rb', line 126

def self.standard_radix(integer_base)
  Radix::Base.standard_radix(integer_base)
end