Class: Tabletop::Die
- Inherits:
-
Object
- Object
- Tabletop::Die
- Includes:
- Comparable
- Defined in:
- lib/tabletop/randomizers.rb
Instance Attribute Summary collapse
-
#sides ⇒ Object
readonly
Returns the value of attribute sides.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(operand) ⇒ Object
Compares based on value of the die.
-
#initialize(params = {}) ⇒ Die
constructor
:sides must be greater then or equal to 1.
-
#roll ⇒ Object
Sets @value to a random number n, where 1 <= n <= @sides.
-
#to_int ⇒ Object
Returns the die’s value.
-
#to_s ⇒ Object
Returns a string in the form “[@value]/d@sides”.
Constructor Details
#initialize(params = {}) ⇒ Die
:sides must be greater then or equal to 1. By default it is 6. If :value is nil, then #roll is called.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/tabletop/randomizers.rb', line 9 def initialize(params={}) if params[:sides].nil? @sides = 6 else @sides = Integer(params[:sides]) raise ArgumentError if @sides < 2 end if params[:value].nil? roll else self.value = params[:value] end end |
Instance Attribute Details
#sides ⇒ Object (readonly)
Returns the value of attribute sides.
5 6 7 |
# File 'lib/tabletop/randomizers.rb', line 5 def sides @sides end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/tabletop/randomizers.rb', line 5 def value @value end |
Class Method Details
Instance Method Details
#<=>(operand) ⇒ Object
Compares based on value of the die
49 50 51 |
# File 'lib/tabletop/randomizers.rb', line 49 def <=>(operand) @value <=> operand.to_int end |
#roll ⇒ Object
Sets @value to a random number n, where 1 <= n <= @sides
32 33 34 |
# File 'lib/tabletop/randomizers.rb', line 32 def roll @value = rand(sides)+1 end |
#to_int ⇒ Object
Returns the die’s value
54 55 56 |
# File 'lib/tabletop/randomizers.rb', line 54 def to_int @value end |
#to_s ⇒ Object
Returns a string in the form “[@value]/d@sides”
37 38 39 |
# File 'lib/tabletop/randomizers.rb', line 37 def to_s "[#{value}]/d#{sides}" end |