class DiceBag::Roll
This is the 'main' class of Dice Bag. This class takes the dice string, parses it, and encapsulates the actual rolling of the dice. If no dice string is given, it defaults to DefaultRoll.
Attributes
dstr[R]
parsed[R]
tree[R]
Public Class Methods
new(dstr = nil)
click to toggle source
# File lib/dicebag/roll.rb, line 16 def initialize(dstr = nil) @dstr = dstr ||= DEFAULT_ROLL @tree = DiceBag.parse dstr @result = nil end
Public Instance Methods
notes()
click to toggle source
# File lib/dicebag/roll.rb, line 22 def notes str = '' tree.each do |_op, part| next unless part.is_a?(RollPart) || !part.notes.empty? str += format('For: %s\n%s\n\n', part, part.notes) end str end
result()
click to toggle source
# File lib/dicebag/roll.rb, line 34 def result roll unless @result @result end
roll()
click to toggle source
# File lib/dicebag/roll.rb, line 40 def roll @label = '' @total = 0 @sections = [] handle_tree @result = Result.new(@label, @total, @sections) end
Private Instance Methods
handle_op(op, part)
click to toggle source
# File lib/dicebag/roll.rb, line 65 def handle_op(op, part) case op when :start then @total = part.total when :add then @total += part.total when :sub then @total -= part.total when :mul then @total *= part.total when :div then @total /= part.total end @sections.push part end
handle_tree()
click to toggle source
# File lib/dicebag/roll.rb, line 52 def handle_tree tree.each do |op, part| if op == :label @label = part.value next end part.roll if part.is_a?(RollPart) # ensure fresh results. handle_op op, part end end