Class: Tabletop::Coin
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
Instance Method Summary collapse
-
#flip ⇒ Object
set to a random value, then return itself.
- #heads? ⇒ Boolean
-
#initialize(params = {}) ⇒ Coin
constructor
A new instance of Coin.
-
#roll ⇒ Object
:nodoc:.
- #tails? ⇒ Boolean
-
#to_s ⇒ Object
Returns either “( )” or “(+)” depending on @value.
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
#flip ⇒ Object
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
104 105 106 |
# File 'lib/tabletop/randomizers.rb', line 104 def heads? @value == 1 end |
#roll ⇒ Object
:nodoc:
94 95 96 |
# File 'lib/tabletop/randomizers.rb', line 94 def roll #:nodoc: @value = rand(sides) end |
#tails? ⇒ Boolean
108 109 110 |
# File 'lib/tabletop/randomizers.rb', line 108 def tails? @value == 0 end |
#to_s ⇒ Object
Returns either “( )” or “(+)” depending on @value
113 114 115 |
# File 'lib/tabletop/randomizers.rb', line 113 def to_s "(#{[' ', '+'][value]})" end |