Class: BagOfHolding::Dice::Die

Inherits:
Object
  • Object
show all
Defined in:
lib/bag_of_holding/dice/die.rb

Overview

Internal: Represent a physical die and it’s attributes

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#explodeObject

Returns the value of attribute explode.



5
6
7
# File 'lib/bag_of_holding/dice/die.rb', line 5

def explode
  @explode
end

#rerollObject

Returns the value of attribute reroll.



5
6
7
# File 'lib/bag_of_holding/dice/die.rb', line 5

def reroll
  @reroll
end

#sidesObject

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

#rollObject



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_sObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/bag_of_holding/dice/die.rb', line 26

def valid?
  DieValidator.valid? self
end