Class: DICE::D

Inherits:
Object
  • Object
show all
Defined in:
lib/toychest/dice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ D

Returns a new instance of D.



5
6
7
8
9
10
# File 'lib/toychest/dice.rb', line 5

def initialize k
	@id = k
	@dice = []
	@roll = 0
	@on = { monopoly: '2d6', yatzee: '5d6', critical: '1d20', luck: '1d8', save: '1d4', percent: '1d100', ratio: '2d10' }
end

Instance Attribute Details

#diceObject

Returns the value of attribute dice.



4
5
6
# File 'lib/toychest/dice.rb', line 4

def dice
  @dice
end

#onObject

Returns the value of attribute on.



4
5
6
# File 'lib/toychest/dice.rb', line 4

def on
  @on
end

#rollObject

Returns the value of attribute roll.



4
5
6
# File 'lib/toychest/dice.rb', line 4

def roll
  @roll
end

Instance Method Details

#[](k) ⇒ Object



32
33
34
# File 'lib/toychest/dice.rb', line 32

def [] k
	run k
end

#run(i, &b) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/toychest/dice.rb', line 11

def run i, &b
	if i.class == Symbol
		ix = @on[i]
	elsif i.class == String
		ix = i
	elsif i.class == Integer
		ix = %[1d#{i}]
	end
	ii = ix.split("d")
	n = ii[0].to_i
	s = ii[1].to_i
	@dice.clear
	@roll = 0
	n.times { x = rand(1..s); @roll += x; @dice << x }
	h = { number: n, sides: s, roll: @roll, dice: @dice }
	if block_given?
		return b.call(h)
	else
		return h
	end
end