Skip to content

Commit

Permalink
Review of documentation from previous merge. [doc]
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Mar 20, 2013
1 parent 91255b0 commit 7d02557
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 226 deletions.
Empty file removed lib/__init__.py
Empty file.
79 changes: 5 additions & 74 deletions lib/radix.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'radix/base'
require 'radix/integer'
require 'radix/float'
require 'radix/rational' # load ?
require 'radix/rational'
require 'radix/operator'

module Radix

##
# Returns the metadata contained in Radix.yml
#
# @return [Hash{String=>String }]
# @return [Hash{String=>String}]
def self.metadata
@metadata ||= (
require 'yaml'
Expand All @@ -20,82 +21,12 @@ def self.metadata
# Gets value of name in metadata or goes up ancestry.
#
# @param [Symbol] name
#
# @return [String]
def self.const_missing(name)
key = name.to_s.downcase
metadata[key] || super(name)
metadata.key?(key) ? metadata[key] : super(name)
end

# @todo Here only for buggy Ruby 1.8.x.
VERSION = metadata['version']
end

##
# Adds the b(base) method to this ruby class for quickly creating Radix
# instances.
class ::Float

##
# Takes a Ruby Float and makes it into a Radix::Float as given base.
#
# @param [Fixnum, Array<String>] base The desired base.
# @return [Radix::Float]
def b(base)
Radix::Float.new(self, base)
end
end

##
# Adds the b(base) method to this ruby class for quickly creating Radix
# instances.
class ::Integer

##
# Takes a Ruby Integer and makes it into a Radix::Integer as given base.
#
# @param [Fixnum, Array<String>] base The desired base.
# @return [Radix::Integer]
def b(base)
Radix::Integer.new(self, base)
end
end

##
# Adds the b(base) method to this ruby class for quickly creating Radix
# instances.
class ::String

##
# Takes a String and makes it into a Radix::Integer or Radix::Float as given
# base. Float is determined by a "." character in string instance
#
# @param [Fixnum, Array<String>] base The desired base.
# @return [Radix::Integer, Radix::Float]
def b(base)
if index('.')
Radix::Float.new(self, base)
else
Radix::Integer.new(self, base)
end
end
end

##
# Adds the b(base) method to this ruby class for quickly creating Radix
# instances.
class ::Array

##
# Takes array and makes it into a Radix::Integer or Radix::Float as given
# base. Float is determined by a "." character in array instance.
#
# @param [Fixnum, Array<String>] base The desired base.
# @return [Radix::Integer, Radix::Float]
def b(base)
if index('.')
Radix::Float.new(self, base)
else
Radix::Integer.new(self, base)
end
end
end
123 changes: 71 additions & 52 deletions lib/radix/base.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
##
# @author: Thomas Sawyer
module Radix

##
# Collection of base contexts.
# Namespace for common bases defined as reusable constants.
#
module BASE
# Array of chars 0 - 9
B10 = ('0'..'9').to_a
Expand All @@ -22,15 +21,29 @@ module BASE
end

##
# The base system and character set to use for number conversions.
# Radix::Base is a functional model of a base system that can be used for
# number conversions.
#
# Radix::Base is the original Radix API. But with the advent of v2.0
# and the new Integer and Float classes, it is essentially deprecated.
#
# @example Convert any base
# b10 = Radix::Base.new(10)
# b10.convert_base([100, 10], 256)
# #=> [2,5,6,1,0]
#
# @example Convert to string notation upto base 62
# b10.convert("10", 62) #=> "62"
#
# @example Odd notations
# b10 = Radix::Base.new(%w{Q W E R T Y U I O U})
# b10.convert("FF", 16) #=> "EYY"
#
# @!attribute [r] chars
# @return [Array<String>] The ASCII character set in use.
# @!attribute [r] base
# @return [Fixnum] The base level in use.
# @!attribute [r] values
# @return [Hash{String=>Fixnum}] A hash of characters and their respective
# value.
# @example Testing base hash default values.
# > test = Radix::Base.new(36)
# > test.values["F"]
Expand All @@ -41,6 +54,8 @@ module BASE
# 34
# > test.values["YY"]
# nil # Fails because "YY" is not a key in the +values+ hash.
# @return [Hash{String=>Fixnum}]
# A hash of characters and their respective value.
class Base

##
Expand All @@ -56,16 +71,19 @@ class Base
attr :base

##
# Hash of characters and values.
#
# @return [Hash{String=>Fixnum}] A hash of characters and their respective
# value.
# @return [Hash{String=>Fixnum}]
# A hash of characters and their respective value.
attr :values

##
# New Radix using +chars+ representation.
#
# @param [Array<String>, Numeric] chars Array of string representation of
# number values of the base or a Numeric of the Base level.
# @param [Array<String>, Numeric] chars
# Array of string representation of number values of the base
# or a Numeric of the Base level.
#
# @return [void]
def initialize(chars=BASE::B62)
if ::Numeric === chars
Expand All @@ -79,11 +97,12 @@ def initialize(chars=BASE::B62)
##
# Convert a value of given radix_base to that of the base instance.
#
# @param [String, Numeric, Array<String>] number The value in "radix_base"
# context.
# @param [Radix::Base, Numeric] radix_base Numeric for the radix or
# instance of Radix::Base.
# @return [String] representation of "number" in self.base level.
# @param [String, Numeric, Array<String>] number
# The value in "radix_base" context.
#
# @param [Radix::Base, Numeric] radix_base
# Numeric for the radix or instance of Radix::Base.
#
# @example Convert Testing (Binary, Decimal, Hex)
# > b = Radix::Base.new(2)
# > d = Radix::Base.new(10)
Expand All @@ -100,6 +119,8 @@ def initialize(chars=BASE::B62)
# "1010"
# > b.convert(10, h)
# "10000"
#
# @return [String] representation of "number" in self.base level.
def convert(number, radix_base)
radix_base = Radix::Base.new(radix_base) unless Radix::Base === radix_base
case number
Expand All @@ -124,10 +145,15 @@ def convert(number, radix_base)
# 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
# @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.
def convert_base(digits, from_base, to_base)
bignum = 0
Expand All @@ -144,7 +170,9 @@ def convert_base(digits, from_base, to_base)
##
# Encode a string in the radix.
#
# @param [String] byte_string String value in this base.
# @param [String] byte_string
# String value in this base.
#
# @return [String] Encoded string from this Base.
def encode(byte_string)
digits = byte_string.unpack("C*")
Expand All @@ -155,7 +183,9 @@ def encode(byte_string)
##
# Decode a string that was previously encoded in the radix.
#
# @param [String] encoded Encoded string from this Base.
# @param [String] encoded
# Encoded string from this Base.
#
# @return [String] Decoded string of value from this base.
def decode(encoded)
digits = encoded.split(//).map{ |c| @values[c] }
Expand All @@ -167,12 +197,17 @@ def decode(encoded)
##
# 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+.
# @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`.
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
Expand All @@ -183,10 +218,15 @@ def self.convert(number, from_base, to_base)
# 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
# @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.
def self.convert_base(digits, from_base, to_base)
bignum = 0
Expand All @@ -202,24 +242,3 @@ def self.convert_base(digits, from_base, to_base)

end

##
# Radix::Base provides the means of converting to and from any base.
#
# b10 = Radix::Base.new(10)
# b10.convert_base([100, 10], 256)
# #=> [2,5,6,1,0]
#
# And it can handle any notation upto base 62.
#
# b10.convert("10", 62) #=> "62"
#
# And the notations need not be in ASCII order --odd notations
# can be used.
#
# b10 = Radix::Base.new(%w{Q W E R T Y U I O U})
# b10.convert("FF", 16) #=> "EYY"ta
#
# NOTE: Radix::Base is the original Radix API. But with the advent of v2.0
# and the new Integer and Float classes, it is outmoded. For now it is here
# for backward compatibility. In a future version it may be deprecated, or
# reworked to serve as the backbone of the other classes.
Loading

0 comments on commit 7d02557

Please sign in to comment.