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
-
.const_missing(name) ⇒ String
Gets value of name in metadata or goes up ancestry.
-
.convert(number, from_base, to_base) ⇒ String
Convert a number of from_base as to_base.
-
.convert_base(digits, from_base, to_base) ⇒ String
Convert any base to any other base, using array of Fixnum’s.
-
.metadata ⇒ Hash{String=>String}
Returns the metadata contained in Radix.yml.
Class Method Details
.const_missing(name) ⇒ String
Gets value of name in metadata or goes up ancestry.
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.
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
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 |