Class: EasyDice

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_dice/version.rb,
lib/easy_dice/easy_dice.rb

Overview

Copyright © 2013 Adam Price (komidore64 at gmail dot com)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Classes: FormatError

Constant Summary collapse

VERSION =
"0.0.2"
READ_FORMAT =

this is the format that EasyDice#read will accept

/\A((\d+)(d(\d+))?\s*[\+\-]\s*)*(\d+)(d(\d+))?\z/i
DIE_PATTERN =

i don’t actually use these named groups, but they make String#scan work like i want it so i’ll leave them in. not complaining.

/(?<count>\d+)(d(?<sides>\d+))?/

Class Method Summary collapse

Class Method Details

.read(str) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easy_dice/easy_dice.rb', line 27

def self.read(str)
  check_format!(str)
  results = str.scan(DIE_PATTERN)
  cleanup(results)

  # top of the linked list
  hand = Dice.new(results[0][0], results[0][1])
  results.shift

  # tack on the rest
  results.each do |res|
    hand += Dice.new(res[0], res[1])
  end
  hand
end