Class: Dice
- Inherits:
-
Object
- Object
- Dice
- Defined in:
- lib/lib/dice.rb
Constant Summary collapse
- @@max_dice =
nil
- @@max_sides =
nil
Instance Attribute Summary collapse
-
#num_of_dice ⇒ Object
Returns the value of attribute num_of_dice.
-
#sides ⇒ Object
Returns the value of attribute sides.
Class Method Summary collapse
- .max_dice=(value) ⇒ Object
- .max_sides=(value) ⇒ Object
-
.parse(string) ⇒ Object
Parses a dice string and returns a dice object More documentation to come later.
Instance Method Summary collapse
-
#initialize(num_of_dice, sides, options = {}) ⇒ Dice
constructor
A new instance of Dice.
- #reroll ⇒ Object
- #results ⇒ Object
Constructor Details
#initialize(num_of_dice, sides, options = {}) ⇒ Dice
Returns a new instance of Dice.
9 10 11 12 13 14 15 |
# File 'lib/lib/dice.rb', line 9 def initialize(num_of_dice,sides, ={}) = () raise ArgumentError, "Your sides exceeds the maximum number of sides allowed" if @@max_sides && sides > @@max_sides raise ArgumentError, "Your dice exceeds the maximum dice allowed" if @@max_dice && num_of_dice > @@max_dice @num_of_dice = num_of_dice @sides = sides end |
Instance Attribute Details
#num_of_dice ⇒ Object
Returns the value of attribute num_of_dice.
3 4 5 |
# File 'lib/lib/dice.rb', line 3 def num_of_dice @num_of_dice end |
#sides ⇒ Object
Returns the value of attribute sides.
3 4 5 |
# File 'lib/lib/dice.rb', line 3 def sides @sides end |
Class Method Details
.max_dice=(value) ⇒ Object
45 46 47 |
# File 'lib/lib/dice.rb', line 45 def self.max_dice=( value ) @@max_dice = value end |
.max_sides=(value) ⇒ Object
49 50 51 |
# File 'lib/lib/dice.rb', line 49 def self.max_sides=( value ) @@max_sides = value end |
.parse(string) ⇒ Object
Parses a dice string and returns a dice object More documentation to come later
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lib/dice.rb', line 34 def self.parse(string) dice_regex = /(?<num_of_dice>\d*)d(?<num_of_sides>\d+)/ dice = dice_regex.match(string) raise ArgumentError, "unable to parse dice string" if dice.nil? num_of_dice = dice[:num_of_dice].to_i num_of_dice = 1 if num_of_dice == 0 num_of_sides = dice[:num_of_sides].to_i dice = Dice.new(num_of_dice,num_of_sides) return dice end |
Instance Method Details
#reroll ⇒ Object
26 27 28 29 |
# File 'lib/lib/dice.rb', line 26 def reroll @results = nil return results end |
#results ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/lib/dice.rb', line 17 def results return @results.inject {|sum,x| sum+=x} if @results @results = [] @num_of_dice.times do @results << rand(sides) + 1 end return @results.inject {|sum,x| sum+=x} end |