Class: Dicey::StaticDie

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

Overview

Note:

Unlike other dice, +roll+ing 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.

Instance Attribute Summary collapse

Attributes inherited from AbstractDie

#sides_list, #sides_num

Instance Method Summary collapse

Methods inherited from AbstractDie

#==, describe, #eql?, 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

#valueAny (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_sString

Return a string representing the die.

Static dice are represented with a "+" or "-" followed by the absolute value (except 0 which doesn't have a sign).

Returns:

  • (String)


34
35
36
# File 'lib/dicey/static_die.rb', line 34

def to_s
  (!@value.respond_to?(:positive?) || @value.positive?) ? "+#{@value}" : @value.to_s
end