Class: Natural20::DieRoll::DieRolls

Inherits:
Object
  • Object
show all
Defined in:
lib/natural_20/die_roll.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rolls = []) ⇒ DieRolls

Returns a new instance of DieRolls.



80
81
82
# File 'lib/natural_20/die_roll.rb', line 80

def initialize(rolls = [])
  @rolls = rolls
end

Instance Attribute Details

#rollsObject

Returns the value of attribute rolls.



78
79
80
# File 'lib/natural_20/die_roll.rb', line 78

def rolls
  @rolls
end

Instance Method Details

#+(other) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/natural_20/die_roll.rb', line 92

def +(other)
  if other.is_a?(Natural20::DieRoll)
    @rolls << other
  elsif other.is_a?(DieRolls)
    @rolls += other.rolls
  end
end

#==(other) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/natural_20/die_roll.rb', line 106

def ==(other)
  return false if other.rolls.size != @rolls.size

  @rolls.each_with_index do |roll, index|
    return false if other.rolls[index] != roll
  end

  true
end

#add_to_front(die_roll) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/natural_20/die_roll.rb', line 84

def add_to_front(die_roll)
  if die_roll.is_a?(Natural20::DieRoll)
    @rolls.unshift(die_roll)
  elsif die_roll.is_a?(DieRolls)
    @rolls = die_roll.rolls + @rolls
  end
end

#resultObject



100
101
102
103
104
# File 'lib/natural_20/die_roll.rb', line 100

def result
  @rolls.inject(0) do |sum, roll|
    sum + roll.result
  end
end

#to_sObject



116
117
118
# File 'lib/natural_20/die_roll.rb', line 116

def to_s
  @rolls.map(&:to_s).join(' + ')
end