Class: Hazard

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

Overview

This class roll the dice.

Author:

  • Cédric ZUGER

Constant Summary collapse

DICE_NAME_REGEXP =
/(d|r|m|s)?(\d*)d(\d+)/.freeze
VERSION =
'1.3.4'

Class Method Summary collapse

Class Method Details

.from_string(dice_string) ⇒ Object

From string entry point, in case you have your dice description in a database for instance.

Parameters:

  • dice_string (String)

    the description of the dice. See help for detail.

Returns:

  • (Object)

    if detail has been asked, it will return a [RolledDice] object, otherwise it will return an [Integer] containing the sum of the dice.



31
32
33
# File 'lib/hazard.rb', line 31

def self.from_string( dice_string )
  roll_dice( dice_string )
end

.lucky?(dice) ⇒ Boolean

Roll a dice and return true if you rolled the highest number

Parameters:

  • dice (Integer)

    the dice you want to roll.

Returns:

  • (Boolean)

    true if you scored the highest number, false otherwise.



45
46
47
# File 'lib/hazard.rb', line 45

def self.lucky?( dice )
  roll_dice( "d#{dice}" ) == dice
end

.method_missing(method_name) ⇒ Object

Regular entry point. This is where you will go when you call Hazard.d6 for instance.

Parameters:

  • method_name (String)

    the description of the dice. See help for detail.

Returns:

  • (Object)

    if detail has been asked, it will return a [RolledDice] object, otherwise it will return an [Integer] containing the sum of the dice.



16
17
18
19
20
21
22
23
24
# File 'lib/hazard.rb', line 16

def self.method_missing( method_name )
  method_name = method_name.to_s

  if method_name =~ DICE_NAME_REGEXP
    roll_dice( method_name )
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Hook the method_missing

Returns:

  • (Boolean)


36
37
38
# File 'lib/hazard.rb', line 36

def self.respond_to_missing?(method_name, include_private = false)
  method_name.to_s =~ DICE_NAME_REGEXP || super
end