Class: Zold::Amount
- Inherits:
-
Object
- Object
- Zold::Amount
- Defined in:
- lib/zold/amount.rb
Overview
Amount
Constant Summary collapse
- MAX =
Maximum amount of zents
2**63
- ZERO =
Just zero, for convenience.
Amount.new(zents: 0)
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
-
#initialize(zents: nil, zld: nil) ⇒ Amount
constructor
A new instance of Amount.
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
-
#to_f ⇒ Object
Convert to ZLD and return as a float.
-
#to_i ⇒ Object
Convert it to zents and return as an integer.
- #to_s ⇒ Object
-
#to_zld(digits = 2) ⇒ Object
Convert to ZLD and return as a string.
- #zero? ⇒ Boolean
Constructor Details
#initialize(zents: nil, zld: nil) ⇒ Amount
Returns a new instance of Amount.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/zold/amount.rb', line 39 def initialize(zents: nil, zld: nil) if !zents.nil? raise "Integer is required, while #{zents.class} provided: #{zents}" unless zents.is_a?(Integer) @zents = zents elsif !zld.nil? raise "Float is required, while #{zld.class} provided: #{zld}" unless zld.is_a?(Float) @zents = (zld * 2**FRACTION).to_i else raise 'You can\'t specify both coints and zld' end raise "The amount is too big: #{@zents}" if @zents > MAX raise "The amount is too small: #{@zents}" if @zents < -MAX end |
Instance Method Details
#*(other) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/zold/amount.rb', line 129 def *(other) raise '* may only work with a number' unless other.is_a?(Integer) || other.is_a?(Float) c = (@zents * other).to_i raise "Overflow, can't multiply #{@zents} by #{m}" if c > MAX Amount.new(zents: c) end |
#+(other) ⇒ Object
107 108 109 110 |
# File 'lib/zold/amount.rb', line 107 def +(other) raise '+ may only work with Amount' unless other.is_a?(Amount) Amount.new(zents: @zents + other.to_i) end |
#-(other) ⇒ Object
112 113 114 115 |
# File 'lib/zold/amount.rb', line 112 def -(other) raise '- may only work with Amount' unless other.is_a?(Amount) Amount.new(zents: @zents - other.to_i) end |
#/(other) ⇒ Object
136 137 138 139 |
# File 'lib/zold/amount.rb', line 136 def /(other) raise '/ may only work with a number' unless other.is_a?(Integer) || other.is_a?(Float) Amount.new(zents: (@zents / other).to_i) end |
#<(other) ⇒ Object
92 93 94 95 |
# File 'lib/zold/amount.rb', line 92 def <(other) raise '< may only work with Amount' unless other.is_a?(Amount) @zents < other.to_i end |
#<=(other) ⇒ Object
97 98 99 100 |
# File 'lib/zold/amount.rb', line 97 def <=(other) raise '<= may only work with Amount' unless other.is_a?(Amount) @zents <= other.to_i end |
#<=>(other) ⇒ Object
102 103 104 105 |
# File 'lib/zold/amount.rb', line 102 def <=>(other) raise '<= may only work with Amount' unless other.is_a?(Amount) @zents <=> other.to_i end |
#==(other) ⇒ Object
82 83 84 85 |
# File 'lib/zold/amount.rb', line 82 def ==(other) raise "== may only work with Amount: #{other}" unless other.is_a?(Amount) @zents == other.to_i end |
#>(other) ⇒ Object
87 88 89 90 |
# File 'lib/zold/amount.rb', line 87 def >(other) raise '> may only work with Amount' unless other.is_a?(Amount) @zents > other.to_i end |
#negative? ⇒ Boolean
121 122 123 |
# File 'lib/zold/amount.rb', line 121 def negative? @zents.negative? end |
#positive? ⇒ Boolean
125 126 127 |
# File 'lib/zold/amount.rb', line 125 def positive? @zents.positive? end |
#to_f ⇒ Object
Convert to ZLD and return as a float.
62 63 64 |
# File 'lib/zold/amount.rb', line 62 def to_f @zents.to_f / 2**FRACTION end |
#to_i ⇒ Object
Convert it to zents and return as an integer.
57 58 59 |
# File 'lib/zold/amount.rb', line 57 def to_i @zents end |
#to_s ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/zold/amount.rb', line 71 def to_s text = "#{to_zld}ZLD" if positive? Rainbow(text).green elsif negative? Rainbow(text).red else text end end |
#to_zld(digits = 2) ⇒ Object
Convert to ZLD and return as a string. If you need float, you should use to_f()
later.
67 68 69 |
# File 'lib/zold/amount.rb', line 67 def to_zld(digits = 2) format("%0.#{digits}f", to_f) end |
#zero? ⇒ Boolean
117 118 119 |
# File 'lib/zold/amount.rb', line 117 def zero? @zents.zero? end |