Class: BagOfHolding::Dice::Die
- Inherits:
-
Object
- Object
- BagOfHolding::Dice::Die
- Defined in:
- lib/bag_of_holding/dice/die.rb
Overview
Internal: Represent a physical die and it’s attributes
Instance Attribute Summary collapse
-
#explode ⇒ Object
Returns the value of attribute explode.
-
#reroll ⇒ Object
Returns the value of attribute reroll.
-
#sides ⇒ Object
Returns the value of attribute sides.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(opts = {}) ⇒ Die
constructor
A new instance of Die.
- #roll ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Die
Returns a new instance of Die.
7 8 9 10 11 |
# File 'lib/bag_of_holding/dice/die.rb', line 7 def initialize(opts = {}) self.sides = opts[:sides] self.explode = opts[:explode] self.reroll = opts[:reroll] end |
Instance Attribute Details
#explode ⇒ Object
Returns the value of attribute explode.
5 6 7 |
# File 'lib/bag_of_holding/dice/die.rb', line 5 def explode @explode end |
#reroll ⇒ Object
Returns the value of attribute reroll.
5 6 7 |
# File 'lib/bag_of_holding/dice/die.rb', line 5 def reroll @reroll end |
#sides ⇒ Object
Returns the value of attribute sides.
5 6 7 |
# File 'lib/bag_of_holding/dice/die.rb', line 5 def sides @sides end |
Instance Method Details
#==(other) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/bag_of_holding/dice/die.rb', line 13 def ==(other) return false if sides != other.sides return false if explode != other.explode return false if reroll != other.reroll true end |
#roll ⇒ Object
21 22 23 24 |
# File 'lib/bag_of_holding/dice/die.rb', line 21 def roll fail StandardError unless valid? BagOfHolding::Dice::DieRoller.roll self end |
#to_s ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/bag_of_holding/dice/die.rb', line 30 def to_s 'd'.tap do |str| str << sides.to_s str << "r#{reroll}" if reroll str << "e#{explode}" if explode end end |
#valid? ⇒ Boolean
26 27 28 |
# File 'lib/bag_of_holding/dice/die.rb', line 26 def valid? DieValidator.valid? self end |