Class: Baron::Money

Inherits:
Object
  • Object
show all
Includes:
Transferrable, Comparable
Defined in:
lib/baron/money.rb

Overview

Money is a fundamental concept in 18xx. It is used to pay for shares, trains, track, token placement, and more.

The winner of an 18xx game is the player who has highest value at the end whish is the sum of the money they have, plus the monetary value of shares they own.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transferrable

#owner=, #validate_owner

Constructor Details

#initialize(amount = 0) ⇒ Money

Initialize the money object

Examples:

Baron::Money.new(10)

Parameters:

  • amount (Fixnum) (defaults to: 0)

29
30
31
32
# File 'lib/baron/money.rb', line 29

def initialize(amount = 0)
  fail ArgumentError unless amount.instance_of?(Fixnum)
  @amount = amount
end

Instance Attribute Details

#amountFixnum (readonly)

The amount of money

Examples:

money.amount

Returns:

  • (Fixnum)

20
21
22
# File 'lib/baron/money.rb', line 20

def amount
  @amount
end

Instance Method Details

#*(other) ⇒ Baron::Money

Multiplies a money by an integer and returns a new money with the result

Examples:

Baron::Money.new(10) * 2 #=> Baron::Money.new(20)

Parameters:

  • other (Fixnum)

    The number to multiply

Returns:


66
67
68
# File 'lib/baron/money.rb', line 66

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

#+(other) ⇒ Baron::Money

Adds two moneys together and returns a new money with the summed value

Examples:

Baron::Money.new(10) + Baron::Money.new(2) #=> Baron::Money.new(12)

Parameters:

Returns:


42
43
44
# File 'lib/baron/money.rb', line 42

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

#-(other) ⇒ Baron::Money

Subtracts one money from another returns a new money with the summed value

Examples:

Baron::Money.new(10) - Baron::Money.new(2) #=> Baron::Money.new(8)

Parameters:

Returns:


54
55
56
# File 'lib/baron/money.rb', line 54

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

#<=>(other) ⇒ Fixnum

Compares the money amounts

object in is greater, 0 if they are equal, and nil if they are not comparable

Examples:

Baron::Money.new(10) > Baron::Money.new(5) #=> true

Parameters:

Returns:

  • (Fixnum)

    Returns 1 if this object is greater, -1 if the other


80
81
82
# File 'lib/baron/money.rb', line 80

def <=>(other)
  amount <=> other.amount
end

#to_intFixnum

Returns an integer representation of the money

Examples:

Baron::Money.new(10).to_str #=> 10

Returns:

  • (Fixnum)

91
92
93
# File 'lib/baron/money.rb', line 91

def to_int
  amount
end

#to_sString

Convert the money to a a string

Examples:

Baron::Money.new(10).to_str #=> '$10'

Returns:

  • (String)

    The string representation of this money


102
103
104
# File 'lib/baron/money.rb', line 102

def to_s
  "$#{amount}"
end