Class: Numerals::Digits
- Inherits:
-
Object
- Object
- Numerals::Digits
- Extended by:
- Forwardable
- Includes:
- ModalSupport::BracketConstructor, ModalSupport::StateEquivalent
- Defined in:
- lib/numerals/digits.rb
Overview
Sequence of digit values, with an Array-compatible interface. Having this encapsulated here allows changing the implementation e.g. to an Integer or packed in a String, …
Instance Attribute Summary collapse
-
#digits_array ⇒ Object
readonly
Returns the value of attribute digits_array.
-
#radix ⇒ Object
readonly
Returns the value of attribute radix.
Instance Method Summary collapse
-
#dup ⇒ Object
Deep copy.
-
#initialize(*args) ⇒ Digits
constructor
A new instance of Digits.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #truncate!(n) ⇒ Object
- #valid? ⇒ Boolean
-
#value ⇒ Object
Integral coefficient.
- #value=(v) ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Digits
Returns a new instance of Digits.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/numerals/digits.rb', line 9 def initialize(*args) if Hash === args.last = args.pop else = {} end @radix = [:base] || [:radix] || 10 if args.size == 1 && Array === args.first @digits_array = args.first else @digits_array = args end if [:value] self.value = [:value] end end |
Instance Attribute Details
#digits_array ⇒ Object (readonly)
Returns the value of attribute digits_array.
28 29 30 |
# File 'lib/numerals/digits.rb', line 28 def digits_array @digits_array end |
#radix ⇒ Object (readonly)
Returns the value of attribute radix.
28 29 30 |
# File 'lib/numerals/digits.rb', line 28 def radix @radix end |
Instance Method Details
#dup ⇒ Object
Deep copy
75 76 77 |
# File 'lib/numerals/digits.rb', line 75 def dup Digits[@digits_array.dup, base: @radix] end |
#inspect ⇒ Object
89 90 91 |
# File 'lib/numerals/digits.rb', line 89 def inspect to_s end |
#to_s ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/numerals/digits.rb', line 79 def to_s args = "" if @digits_array.size > 0 args << @digits_array.to_s.unwrap('[]') args << ', ' end args << "base: #{radix}" "Digits[#{args}]" end |
#truncate!(n) ⇒ Object
93 94 95 |
# File 'lib/numerals/digits.rb', line 93 def truncate!(n) @digits_array.slice! n..-1 end |
#valid? ⇒ Boolean
97 98 99 |
# File 'lib/numerals/digits.rb', line 97 def valid? @digits_array.none? { |x| !x.kind_of?(Integer) || x < 0 || x >= @radix } end |
#value ⇒ Object
Integral coefficient
45 46 47 48 49 50 51 |
# File 'lib/numerals/digits.rb', line 45 def value if @radix == 10 @digits_array.join.to_i else @digits_array.inject(0){|x,y| x*@radix + y} end end |
#value=(v) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/numerals/digits.rb', line 53 def value=(v) raise "Invalid digits value" if v < 0 if @radix < 37 replace v.to_s(@radix).each_char.map{|c| c.to_i(@radix)} else if v == 0 replace [0] else while v > 0 v, r = v.divmod(@radix) unshift r end end end end |
#zero? ⇒ Boolean
69 70 71 72 |
# File 'lib/numerals/digits.rb', line 69 def zero? # value == 0 !@digits_array || @digits_array.empty? || @digits_array.all?{|d| d==0 } end |