Class: Dicey::RegularDie

Inherits:
NumericDie show all
Defined in:
lib/dicey/regular_die.rb

Overview

Regular die, which has N sides with numbers from 1 to N.

Constant Summary collapse

D6 =

Characters to use for small dice.

"⚀⚁⚂⚃⚄⚅"

Instance Attribute Summary

Attributes inherited from AbstractDie

#sides_list, #sides_num

Instance Method Summary collapse

Methods inherited from AbstractDie

#==, #current, describe, #eql?, from_count, from_list, #hash, #next, rand, #roll, srand

Constructor Details

#initialize(max) ⇒ RegularDie

Returns a new instance of RegularDie.

Parameters:

  • max (Integer)

    maximum side / number of sides



12
13
14
15
16
17
18
# File 'lib/dicey/regular_die.rb', line 12

def initialize(max)
  unless Integer === max && max.positive?
    raise DiceyError, "regular dice can contain only positive integers, #{max.inspect} is not"
  end

  super((1..max))
end

Instance Method Details

#to_sString

Dice with 1–6 sides are displayed with a single character from D6. More than that, and we get into the square bracket territory.

Returns:

  • (String)


24
25
26
# File 'lib/dicey/regular_die.rb', line 24

def to_s
  (sides_num <= D6.size) ? D6[sides_num - 1] : "[#{sides_num}]"
end