Class: NAdicNumber::Base
- Inherits:
-
Object
- Object
- NAdicNumber::Base
- Defined in:
- lib/n_adic_number/base.rb
Class Attribute Summary collapse
-
.base_num ⇒ Object
readonly
Returns the value of attribute base_num.
-
.map_table ⇒ Object
readonly
Returns the value of attribute map_table.
-
.reverse_map ⇒ Object
readonly
Returns the value of attribute reverse_map.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(seed) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(seed) ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/n_adic_number/base.rb', line 24 def initialize(seed) raise "Not define map_table" unless base_num case seed when Integer raise ArgumentError if seed < 0 @integer = seed when String @str_chars = seed.reverse.chars raise ArgumentError if @str_chars.one?{|ch| !reverse_map.has_key?(ch)} @string = seed else raise ArgumentError end end |
Class Attribute Details
.base_num ⇒ Object (readonly)
Returns the value of attribute base_num.
4 5 6 |
# File 'lib/n_adic_number/base.rb', line 4 def base_num @base_num end |
.map_table ⇒ Object (readonly)
Returns the value of attribute map_table.
4 5 6 |
# File 'lib/n_adic_number/base.rb', line 4 def map_table @map_table end |
.reverse_map ⇒ Object (readonly)
Returns the value of attribute reverse_map.
4 5 6 |
# File 'lib/n_adic_number/base.rb', line 4 def reverse_map @reverse_map end |
Class Method Details
.keta(pos) ⇒ Object
19 20 21 |
# File 'lib/n_adic_number/base.rb', line 19 def keta(pos) @keta[pos] ||= @base_num ** pos end |
.mapping(ary) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/n_adic_number/base.rb', line 6 def mapping(ary) raise ArgumentError, "map_table is not Array" unless ary.is_a?(Array) raise ArgumentError, "map_table is empty" if ary.empty? raise ArgumentError, "map_table is not unique" if ary.uniq! @map_table = ary @base_num = ary.size @reverse_map = {} ary.each_with_index{|ch,i| @reverse_map[ch] = i} @keta = (0..9).map{|i| @base_num ** i} nil end |
Instance Method Details
#inspect ⇒ Object
55 56 57 |
# File 'lib/n_adic_number/base.rb', line 55 def inspect %Q|#<#{self.class.name}:0x#{"%014x" % (self.object_id << 1)} base:#{base_num} num:#{to_s} integer:#{to_i}>| end |
#to_i ⇒ Object
44 45 46 |
# File 'lib/n_adic_number/base.rb', line 44 def to_i @integer ||= make_integer end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/n_adic_number/base.rb', line 40 def to_s @string ||= make_string end |