Class: Dicey::NumericDie

Inherits:
AbstractDie show all
Defined in:
lib/dicey/numeric_die.rb

Overview

A die which only has numeric sides, with no shenanigans.

The only inherent difference in behavior compared to AbstractDie is that this class checks values for sides on initialization. AbstractDie may be rejected where only numeric dice are expected.

Direct Known Subclasses

RegularDie

Constant Summary

Constants inherited from AbstractDie

AbstractDie::STRING_TO_QUOTE

Instance Attribute Summary

Attributes inherited from AbstractDie

#sides_list, #sides_num

Instance Method Summary collapse

Methods inherited from AbstractDie

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

Constructor Details

#initialize(sides_list) ⇒ NumericDie

Returns a new instance of NumericDie.

Parameters:

  • sides_list (Array<Numeric>, Range<Numeric>, Enumerable<Numeric>)

Raises:

  • (DiceyError) —

    if sides_list contains non-numerical values or is empty



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dicey/numeric_die.rb', line 14

def initialize(sides_list)
  if Range === sides_list
    unless Integer === sides_list.begin && Integer === sides_list.end
      raise DiceyError, "`#{sides_list.inspect}` is not a valid range!"
    end

    @range = sides_list
  else
    sides_list.each do |value|
      raise DiceyError, "`#{value.inspect}` is not a number!" unless Numeric === value
    end
  end

  super
end

Instance Method Details

#numeric? ⇒ true

Whether all sides of this die are Numeric.

Returns:

  • (true)


33
34
35
# File 'lib/dicey/numeric_die.rb', line 33

def numeric?
  true
end

#to_s ⇒ String

Return a string representing the die.

Default representation is a list of sides in round brackets. If the die was initialized with a Range, string will be in the form of begin..end.

Returns:

  • (String)


43
44
45
46
47
# File 'lib/dicey/numeric_die.rb', line 43

def to_s
  return "#{@range.begin}..#{@range.end}" if @range

  super
end