Class: SelfDefense
- Inherits:
-
Object
- Object
- SelfDefense
- Defined in:
- lib/self-defense.rb
Overview
file: self-defense.rb
Constant Summary collapse
- @@hits =
[Time.now - 1]
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize {|_self| ... } ⇒ SelfDefense
constructor
A new instance of SelfDefense.
- #rapid?(recovery_period = 0.8) ⇒ Boolean
- #sustained?(h = {}) ⇒ Boolean
Constructor Details
#initialize {|_self| ... } ⇒ SelfDefense
Returns a new instance of SelfDefense.
9 10 11 12 13 |
# File 'lib/self-defense.rb', line 9 def initialize(&block) super() @unit = {s: proc{|x| x}, m: proc{|x| x*60}, h: proc{|x| x*60*60}} yield(self) end |
Class Method Details
.new ⇒ Object
15 16 17 18 |
# File 'lib/self-defense.rb', line 15 def self.new super() @@hits << Time.now end |
Instance Method Details
#rapid?(recovery_period = 0.8) ⇒ Boolean
20 21 22 |
# File 'lib/self-defense.rb', line 20 def rapid?(recovery_period=0.8) (Time.now - @@hits.last) > recovery_period end |
#sustained?(h = {}) ⇒ Boolean
24 25 26 27 |
# File 'lib/self-defense.rb', line 24 def sustained?(h={}) duration = @unit[h[:duration].slice!(-1).to_sym].call(h[:duration]) @@hits.select {|x| @@hits.last - x <= duration.to_i}.length < h[:hit_limit] + 1 end |