Class: BBMB::Util::Money

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/bbmb/util/numbers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount) ⇒ Money

Returns a new instance of Money.



36
37
38
# File 'lib/bbmb/util/numbers.rb', line 36

def initialize(amount)
  @credits = (amount.to_f * 100).round
end

Instance Attribute Details

#creditsObject (readonly)

Returns the value of attribute credits.



34
35
36
# File 'lib/bbmb/util/numbers.rb', line 34

def credits
  @credits
end

Instance Method Details

#*(other) ⇒ Object



51
52
53
# File 'lib/bbmb/util/numbers.rb', line 51

def *(other)
  Money.new(to_f * other.to_f)
end

#+(other) ⇒ Object



45
46
47
# File 'lib/bbmb/util/numbers.rb', line 45

def +(other)
  Money.new(to_f + other.to_f)
end

#-(other) ⇒ Object



48
49
50
# File 'lib/bbmb/util/numbers.rb', line 48

def -(other)
  Money.new(to_f - other.to_f)
end

#/(other) ⇒ Object



54
55
56
# File 'lib/bbmb/util/numbers.rb', line 54

def /(other)
  Money.new(to_f / other.to_f)
end

#<=>(other) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/bbmb/util/numbers.rb', line 57

def <=>(other)
  case other
  when Money
    @credits <=> other.credits
  else
    to_f <=> other.to_f
  end
end

#to_fObject



39
40
41
# File 'lib/bbmb/util/numbers.rb', line 39

def to_f
  @credits.to_f / 100
end

#to_sObject



42
43
44
# File 'lib/bbmb/util/numbers.rb', line 42

def to_s
  sprintf("%1.2f", to_f)
end