Class: Zold::Amount
- Inherits:
-
Object
- Object
- Zold::Amount
- Defined in:
- lib/zold/amount.rb
Overview
Amount
Constant Summary collapse
- ZERO =
Amount.new(coins: 0)
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(coins: nil, zld: nil) ⇒ Amount
constructor
A new instance of Amount.
- #mul(m) ⇒ Object
- #negative? ⇒ Boolean
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_zld ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(coins: nil, zld: nil) ⇒ Amount
Returns a new instance of Amount.
30 31 32 33 34 35 |
# File 'lib/zold/amount.rb', line 30 def initialize(coins: nil, zld: nil) raise 'You can\'t specify both coints and zld' if !coins.nil? && !zld.nil? @coins = coins unless coins.nil? @coins = (zld * 2**24).to_i unless zld.nil? raise "Integer is required: #{@coins.class}" unless @coins.is_a?(Integer) end |
Instance Method Details
#+(other) ⇒ Object
60 61 62 |
# File 'lib/zold/amount.rb', line 60 def +(other) Amount.new(coins: @coins + other.to_i) end |
#==(other) ⇒ Object
56 57 58 |
# File 'lib/zold/amount.rb', line 56 def ==(other) @coins == other.to_i end |
#mul(m) ⇒ Object
72 73 74 75 76 |
# File 'lib/zold/amount.rb', line 72 def mul(m) c = @coins * m raise "Overflow, can't multiply #{@coins} by #{m}" if c > 2**63 Amount.new(coins: c) end |
#negative? ⇒ Boolean
68 69 70 |
# File 'lib/zold/amount.rb', line 68 def negative? @coins < 0 end |
#to_i ⇒ Object
39 40 41 |
# File 'lib/zold/amount.rb', line 39 def to_i @coins end |
#to_s ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/zold/amount.rb', line 47 def to_s text = "#{to_zld}ZLD" if negative? Rainbow(text).red else Rainbow(text).green end end |
#to_zld ⇒ Object
43 44 45 |
# File 'lib/zold/amount.rb', line 43 def to_zld format('%0.2f', @coins.to_f / 2**24) end |
#zero? ⇒ Boolean
64 65 66 |
# File 'lib/zold/amount.rb', line 64 def zero? @coins.zero? end |