Class: Tools::Die

Inherits:
Object
  • Object
show all
Defined in:
app/models/tools/die.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#sidesObject

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.

Returns:

  • (Boolean)


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_sObject

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