Class: Dicey::AbstractDie
- Inherits:
-
Object
- Object
- Dicey::AbstractDie
- Defined in:
- lib/dicey/abstract_die.rb
Overview
Asbtract die which may have an arbitrary list of sides, not even neccessarily numbers (but preferably so).
Direct Known Subclasses
Constant Summary collapse
- @@random =
Yes, class variable is actually useful here. TODO: Allow supplying a custom Random.
Random.new
Instance Attribute Summary collapse
-
#sides_list ⇒ Object
readonly
Returns the value of attribute sides_list.
-
#sides_num ⇒ Object
readonly
Returns the value of attribute sides_num.
Class Method Summary collapse
-
.describe(dice) ⇒ String
Get a text representation of a list of dice.
-
.from_count(count, definition) ⇒ Array<AbstractDie>
Create a number of equal dice.
-
.from_list(*definitions) ⇒ Array<AbstractDie>
Create a bunch of different dice at once.
-
.rand ⇒ Object
private
Get a random value using a private instance of Random.
-
.srand ⇒ Object
Reset internal randomizer using a new seed.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Determine if this die and the other one have the same list of sides.
-
#current ⇒ Any
Get current side of the die.
-
#eql?(other) ⇒ Boolean
Determine if this die and the other one are of the same class and have the same list of sides.
-
#hash ⇒ Integer
Generates an Integer hash value for this object.
-
#initialize(sides_list) ⇒ AbstractDie
constructor
A new instance of AbstractDie.
-
#next ⇒ Any
Get next side of the die, advancing internal state.
-
#roll ⇒ Any
Move internal state to a random side.
- #to_s ⇒ String
Constructor Details
#initialize(sides_list) ⇒ AbstractDie
Returns a new instance of AbstractDie.
62 63 64 65 66 67 68 |
# File 'lib/dicey/abstract_die.rb', line 62 def initialize(sides_list) @sides_list = (Array === sides_list) ? sides_list.dup.freeze : sides_list.to_a.freeze raise DiceyError, "dice must have at least one side!" if @sides_list.empty? @sides_num = @sides_list.size @current_side_index = 0 end |
Instance Attribute Details
#sides_list ⇒ Object (readonly)
Returns the value of attribute sides_list.
58 59 60 |
# File 'lib/dicey/abstract_die.rb', line 58 def sides_list @sides_list end |
#sides_num ⇒ Object (readonly)
Returns the value of attribute sides_num.
58 59 60 |
# File 'lib/dicey/abstract_die.rb', line 58 def sides_num @sides_num end |
Class Method Details
.describe(dice) ⇒ String
Get a text representation of a list of dice.
32 33 34 35 36 37 |
# File 'lib/dicey/abstract_die.rb', line 32 def self.describe(dice) return dice.to_s if AbstractDie === dice return dice.join(";") if Array === dice dice.to_a.join(";") end |
.from_count(count, definition) ⇒ Array<AbstractDie>
Create a number of equal dice.
54 55 56 |
# File 'lib/dicey/abstract_die.rb', line 54 def self.from_count(count, definition) Array.new(count) { new(definition) } end |
.from_list(*definitions) ⇒ Array<AbstractDie>
Create a bunch of different dice at once.
44 45 46 |
# File 'lib/dicey/abstract_die.rb', line 44 def self.from_list(*definitions) definitions.map { new(_1) } end |
.rand ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get a random value using a private instance of Random.
12 13 14 |
# File 'lib/dicey/abstract_die.rb', line 12 def self.rand(...) @@random.rand(...) end |
.srand ⇒ Object
Reset internal randomizer using a new seed.
18 19 20 |
# File 'lib/dicey/abstract_die.rb', line 18 def self.srand(...) @@random = Random.new(...) end |
Instance Method Details
#==(other) ⇒ Boolean
Determine if this die and the other one have the same list of sides. Be aware that differently ordered sides are not considered equal.
105 106 107 |
# File 'lib/dicey/abstract_die.rb', line 105 def ==(other) AbstractDie === other && same_sides?(other) end |
#current ⇒ Any
Get current side of the die.
73 74 75 |
# File 'lib/dicey/abstract_die.rb', line 73 def current @sides_list[@current_side_index] end |
#eql?(other) ⇒ Boolean
Determine if this die and the other one are of the same class and have the same list of sides. Be aware that differently ordered sides are not considered equal.
die_1.eql?(die_2) implies die_1.hash == die_2.hash.
117 118 119 |
# File 'lib/dicey/abstract_die.rb', line 117 def eql?(other) self.class === other && same_sides?(other) end |
#hash ⇒ Integer
Generates an Integer hash value for this object.
124 125 126 |
# File 'lib/dicey/abstract_die.rb', line 124 def hash [self.class, @sides_list].hash end |
#next ⇒ Any
Get next side of the die, advancing internal state. Starts from first side, wraps from last to first side.
81 82 83 84 85 |
# File 'lib/dicey/abstract_die.rb', line 81 def next ret = current @current_side_index = (@current_side_index + 1) % @sides_num ret end |
#roll ⇒ Any
Move internal state to a random side.
90 91 92 93 |
# File 'lib/dicey/abstract_die.rb', line 90 def roll @current_side_index = self.class.rand(0...@sides_num) current end |
#to_s ⇒ String
96 97 98 |
# File 'lib/dicey/abstract_die.rb', line 96 def to_s "(#{@sides_list.join(",")})" end |