Class: Dicey::StaticDie

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

Overview

Note:

Unlike other dice, rolling a static die does not advance shared randomness generator, thus it does not have any impact on rolling other dice.

Static die has only one side and always returns the same value. Useful to model constants in dice expressions.

Constant Summary

Constants inherited from AbstractDie

AbstractDie::STRING_TO_QUOTE

Instance Attribute Summary collapse

Attributes inherited from AbstractDie

#sides_list, #sides_num

Instance Method Summary collapse

Methods inherited from AbstractDie

#==, describe, #eql?, #freeze, from_count, from_list, #hash, #numeric?, rand, srand

Constructor Details

#initialize(value) ⇒ StaticDie

Returns a new instance of StaticDie.

Parameters:

  • value (Any)


19
20
21
22
# File 'lib/dicey/static_die.rb', line 19

def initialize(value)
  @value = value
  super([@value].freeze)
end

Instance Attribute Details

#value ⇒ Any (readonly) Also known as: current, next, roll

Die's only value.

Returns:

  • (Any)


16
17
18
# File 'lib/dicey/static_die.rb', line 16

def value
  @value
end

Instance Method Details

#to_s ⇒ String

Return a string representing the die.

Static dice are represented with a "+" or "-" followed by the absolute value (0 is represented as +0). Strings are quoted.

Returns:

  • (String)


34
35
36
37
38
39
40
# File 'lib/dicey/static_die.rb', line 34

def to_s
  if @value.respond_to?(:negative?) && @value.negative?
    side_to_s(@value)
  else
    "+#{side_to_s(@value)}"
  end
end