module Radix
Constants
- DIV
Division character for rational numbers
- DIVIDER
Default seperator character.
- DOT
Radix
separator used in string and array representations.
Public Class Methods
Gets value of name in metadata or goes up ancestry.
@param [Symbol] name
@return [String]
# File lib/radix.rb, line 25 def self.const_missing(name) key = name.to_s.downcase metadata.key?(key) ? metadata[key] : super(name) end
Convert a number of from_base as to_base.
@param [String, Numeric
, Array
<String>] number
The value in context of "radix_base".
@param [Fixnum, Radix::Base
] from_base
Source Base
@param [Fixnum, Radix::Base
] to_base
Destination Base
@return [String]
The value of `digits` in `from_base` converted into `to_base`.
# 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 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
@param [Array<Fixnum>] digits
Array of values for each digit of source base.
@param [Fixnum] from_base
Source Base
@param [Fixnum] to_base
Destination Base
@return [String] The value of digits in from_base converted as to_base.
# 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
Returns the metadata contained in Radix.yml
@return [Hash{String=>String}]
# File lib/radix.rb, line 12 def self.metadata @metadata ||= ( require 'yaml' YAML.load(File.new(File.dirname(__FILE__) + '/radix.yml')) ) end