Class: Riak::Client::Decaying
Overview
A float value which decays exponentially toward 0 over time.
Instance Attribute Summary collapse
-
#e ⇒ Object
Returns the value of attribute e.
-
#p ⇒ Object
Returns the value of attribute p.
Instance Method Summary collapse
-
#<<(d) ⇒ Object
Add to current value.
-
#initialize(opts = {}) ⇒ Decaying
constructor
A new instance of Decaying.
-
#value ⇒ Float
The current value (adjusted for the time decay).
Constructor Details
#initialize(opts = {}) ⇒ Decaying
Returns a new instance of Decaying.
14 15 16 17 18 19 |
# File 'lib/riak/client/decaying.rb', line 14 def initialize(opts = {}) @p = opts[:p] || 0.0 @e = opts[:e] || Math::E @r = opts[:r] || Math.log(0.5) / 10 @t0 = Time.now end |
Instance Attribute Details
#e ⇒ Object
Returns the value of attribute e.
6 7 8 |
# File 'lib/riak/client/decaying.rb', line 6 def e @e end |
#p ⇒ Object
Returns the value of attribute p.
7 8 9 |
# File 'lib/riak/client/decaying.rb', line 7 def p @p end |
Instance Method Details
#<<(d) ⇒ Object
Add to current value.
23 24 25 |
# File 'lib/riak/client/decaying.rb', line 23 def <<(d) @p = value + d end |
#value ⇒ Float
Returns the current value (adjusted for the time decay).
28 29 30 31 32 33 |
# File 'lib/riak/client/decaying.rb', line 28 def value now = Time.now dt = now - @t0 @t0 = now @p = @p * (@e ** (@r * dt)) end |