Class: Tools::Die
- Inherits:
-
Object
- Object
- Tools::Die
- Defined in:
- app/models/tools/die.rb
Instance Attribute Summary collapse
-
#sides ⇒ Object
Returns the value of attribute sides.
Instance Method Summary collapse
-
#eql?(die) ⇒ Boolean
Return true if the dice are equal, i.e.
-
#initialize(sides = 20) ⇒ Die
constructor
Creates a new
Die
with aFixnum
as the number ofsides
. -
#roll(rolls = 1) ⇒ Object
Rolls the Die and returns the result as
Fixnum
if rolls==1 or asArray
ofFixnum
if rolls>1. -
#to_s ⇒ Object
Returns the Die as a string with the format Dx, where x is the number of sides.
Constructor Details
#initialize(sides = 20) ⇒ Die
Creates a new Die
with a Fixnum
as the number of sides
5 6 7 |
# File 'app/models/tools/die.rb', line 5 def initialize sides=20 @sides = sides end |
Instance Attribute Details
#sides ⇒ Object
Returns the value of attribute sides.
3 4 5 |
# File 'app/models/tools/die.rb', line 3 def sides @sides end |
Instance Method Details
#eql?(die) ⇒ Boolean
Return true if the dice are equal, i.e. they have the same number of sides.
26 27 28 29 |
# File 'app/models/tools/die.rb', line 26 def eql? die return false if die==nil or die.sides!=@sides return true end |
#roll(rolls = 1) ⇒ Object
Rolls the Die and returns the result as Fixnum
if rolls==1 or as Array
of Fixnum
if rolls>1.
11 12 13 14 15 16 17 18 |
# File 'app/models/tools/die.rb', line 11 def roll rolls=1 return simple_roll if rolls==1 results = Array.new rolls.times do results << simple_roll end return results end |
#to_s ⇒ Object
Returns the Die as a string with the format Dx, where x is the number of sides. Example “D6”, “D20”, “D100”, etc.
21 22 23 |
# File 'app/models/tools/die.rb', line 21 def to_s return "D#{@sides}" end |