class NerdDice::Die

The NerdDice::Die class allows you to instantiate and roll a die by specifying the number of sides with other options. As part of initialization the die will be rolled and have a value

Usage:

Instantiate a d20:
<tt>die = NerdDice::Die.new(20)
    die.value # between 1 and 20
</tt>

You can also specify options when instantiating
<tt>NerdDice::Die.new(12, randomization_technique: :randomized,
                     foreground_color: "#FF0000",
                     background_color: "#FFFFFF",
                     damage_type: "necrotic"))
</tt>

Attributes

background_color[RW]
damage_type[RW]
foreground_color[RW]
included_in_total?[RW]
is_included_in_total[RW]
number_of_sides[R]
value[R]

Public Class Methods

new(number_of_sides, **opts) click to toggle source
# File lib/nerd_dice/die.rb, line 41
def initialize(number_of_sides, **opts)
  @number_of_sides = number_of_sides
  self.randomization_technique = opts[:randomization_technique]
  @background_color = opts[:background_color] || NerdDice.configuration.die_background_color
  @foreground_color = opts[:foreground_color] || NerdDice.configuration.die_foreground_color
  @damage_type = opts[:damage_type]
  @is_included_in_total = true
  roll
end

Public Instance Methods

<=>(other) click to toggle source

comparison operator override using value: required to implement Comparable

# File lib/nerd_dice/die.rb, line 28
def <=>(other)
  value <=> other.value
end
roll() click to toggle source

rolls the die, setting the value to the new roll and returning that value

# File lib/nerd_dice/die.rb, line 33
def roll
  @value = NerdDice.execute_die_roll(@number_of_sides, @randomization_technique)
end