Class: Tabletop::Coin

Inherits:
Die
  • Object
show all
Defined in:
lib/tabletop/randomizers.rb

Overview

A coin is a kind of two-sided Die that has a value of either 0 or 1

Instance Attribute Summary

Attributes inherited from Die

#sides, #value

Instance Method Summary collapse

Methods inherited from Die

#<=>, new_from_string, #to_int

Constructor Details

#initialize(params = {}) ⇒ Coin

Returns a new instance of Coin.



90
91
92
# File 'lib/tabletop/randomizers.rb', line 90

def initialize(params={})
  super(sides: 2, value: params[:value])
end

Instance Method Details

#flipObject

set to a random value, then return itself



99
100
101
102
# File 'lib/tabletop/randomizers.rb', line 99

def flip
  roll
  self
end

#heads?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/tabletop/randomizers.rb', line 104

def heads?
  @value == 1
end

#rollObject

:nodoc:



94
95
96
# File 'lib/tabletop/randomizers.rb', line 94

def roll #:nodoc:
  @value = rand(sides)
end

#tails?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/tabletop/randomizers.rb', line 108

def tails?
  @value == 0
end

#to_sObject

Returns either “( )” or “(+)” depending on @value



113
114
115
# File 'lib/tabletop/randomizers.rb', line 113

def to_s
  "(#{[' ', '+'][value]})"
end