class Zold::Amount

Amount

Constants

FRACTION

How many zents are in one ZLD: 2^FRACTION

MAX

Maximum amount of zents

ZERO

Just zero, for convenience.

Public Class Methods

new(zents: nil, zld: nil) click to toggle source
# 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

Public Instance Methods

*(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/zold/amount.rb, line 121
def negative?
  @zents.negative?
end
positive?() click to toggle source
# File lib/zold/amount.rb, line 125
def positive?
  @zents.positive?
end
to_f() click to toggle source

Convert to ZLD and return as a float.

# File lib/zold/amount.rb, line 62
def to_f
  @zents.to_f / 2**FRACTION
end
to_i() click to toggle source

Convert it to zents and return as an integer.

# File lib/zold/amount.rb, line 57
def to_i
  @zents
end
to_s() click to toggle source
# 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) click to toggle source

Convert to ZLD and return as a string. If you need float, you should use to_f() later.

# File lib/zold/amount.rb, line 67
def to_zld(digits = 2)
  format("%0.#{digits}f", to_f)
end
zero?() click to toggle source
# File lib/zold/amount.rb, line 117
def zero?
  @zents.zero?
end