Class: Randsum::Roll

Inherits:
Object
  • Object
show all
Defined in:
lib/randsum/roll.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(die:, quantity:, result: nil) ⇒ Roll

Returns a new instance of Roll.



14
15
16
17
18
19
# File 'lib/randsum/roll.rb', line 14

def initialize(die:, quantity:, result: nil)
  @die = die
  @quantity = quantity
  @sides = die.sides
  @result = result || roll!
end

Instance Attribute Details

#dieObject (readonly)

Returns the value of attribute die.



4
5
6
# File 'lib/randsum/roll.rb', line 4

def die
  @die
end

#quantityObject (readonly) Also known as: length, count

Returns the value of attribute quantity.



4
5
6
# File 'lib/randsum/roll.rb', line 4

def quantity
  @quantity
end

#resultObject (readonly) Also known as: rolls

Returns the value of attribute result.



4
5
6
# File 'lib/randsum/roll.rb', line 4

def result
  @result
end

#sidesObject (readonly)

Returns the value of attribute sides.



4
5
6
# File 'lib/randsum/roll.rb', line 4

def sides
  @sides
end

#totalObject (readonly) Also known as: to_i

Returns the value of attribute total.



4
5
6
# File 'lib/randsum/roll.rb', line 4

def total
  @total
end

Class Method Details

.roll(num, d:) ⇒ Object



10
11
12
# File 'lib/randsum/roll.rb', line 10

def self.roll(num, d:)
  new(die: Die.new(d), quantity: num)
end

Instance Method Details

#beats?(check_value) ⇒ Boolean

Returns:

  • (Boolean)


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

def beats?(check_value)
  total > check_value
end

#double_all(target) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/randsum/roll.rb', line 47

def double_all(target)
  Replacer.for(
    target: target,
    with: ReplacerValue::DOUBLE,
    roll: self
  ).filter
end

#drop(quantity:, extremity:) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/randsum/roll.rb', line 63

def drop(quantity:,extremity:)
  Dropper.for(
    quantity: quantity,
    extremity: extremity,
    roll: self
  ).filter
end

#drop_highest(quantity = 1) ⇒ Object



75
76
77
# File 'lib/randsum/roll.rb', line 75

def drop_highest(quantity = 1)
  drop(quantity: quantity, extremity: :highest)
end

#drop_lowest(quantity = 1) ⇒ Object



71
72
73
# File 'lib/randsum/roll.rb', line 71

def drop_lowest(quantity = 1)
  drop(quantity: quantity, extremity: :lowest)
end

#meets?(meet_value) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/randsum/roll.rb', line 35

def meets?(meet_value)
  total >= meet_value
end

#replace(target, with:) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/randsum/roll.rb', line 39

def replace(target, with:)
  Replacer.for(
    target: target,
    with: with,
    roll: self
  ).filter
end

#rerollObject



55
56
57
58
59
60
61
# File 'lib/randsum/roll.rb', line 55

def reroll
  Replacer.for(
    target: ReplacerTarget::ALL,
    with: ReplacerValue::REROLL,
    roll: self
  ).filter
end

#to_sObject Also known as: inspect



21
22
23
# File 'lib/randsum/roll.rb', line 21

def to_s
  "You rolled #{count} #{die}, and got #{total}. (Rolls: #{result})"
end