Class: Dicechucker::Dice
- Inherits:
-
Object
- Object
- Dicechucker::Dice
- Defined in:
- lib/dicechucker/dice.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#modifier ⇒ Object
Returns the value of attribute modifier.
-
#number_of_dice ⇒ Object
Returns the value of attribute number_of_dice.
-
#results ⇒ Object
Returns the value of attribute results.
-
#sides ⇒ Object
Returns the value of attribute sides.
-
#total ⇒ Object
Returns the value of attribute total.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(dice, sides, modifier) ⇒ Dice
constructor
A new instance of Dice.
- #report ⇒ Object
- #roll ⇒ Object
Constructor Details
#initialize(dice, sides, modifier) ⇒ Dice
Returns a new instance of Dice.
6 7 8 9 10 11 |
# File 'lib/dicechucker/dice.rb', line 6 def initialize(dice, sides, modifier) @number_of_dice = dice @sides = sides @modifier = modifier roll end |
Instance Attribute Details
#modifier ⇒ Object
Returns the value of attribute modifier.
4 5 6 |
# File 'lib/dicechucker/dice.rb', line 4 def modifier @modifier end |
#number_of_dice ⇒ Object
Returns the value of attribute number_of_dice.
4 5 6 |
# File 'lib/dicechucker/dice.rb', line 4 def number_of_dice @number_of_dice end |
#results ⇒ Object
Returns the value of attribute results.
4 5 6 |
# File 'lib/dicechucker/dice.rb', line 4 def results @results end |
#sides ⇒ Object
Returns the value of attribute sides.
4 5 6 |
# File 'lib/dicechucker/dice.rb', line 4 def sides @sides end |
#total ⇒ Object
Returns the value of attribute total.
4 5 6 |
# File 'lib/dicechucker/dice.rb', line 4 def total @total end |
Instance Method Details
#==(other) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/dicechucker/dice.rb', line 30 def ==(other) [@number_of_dice == other.number_of_dice, @sides == other.sides, @modifier == other.modifier, self.class == other.class].all? end |
#report ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dicechucker/dice.rb', line 18 def report rep = 'rolled ' rep << @results.join(', ') if @modifier > 0 rep << " plus #{@modifier}" elsif @modifier < 0 rep << " minus #{@modifier}" end rep << " for a total of #{@total}." rep end |
#roll ⇒ Object
13 14 15 16 |
# File 'lib/dicechucker/dice.rb', line 13 def roll roll_dice @total = @results.inject(:+) + @modifier end |