Class: SimpleMa

Inherits:
Object
  • Object
show all
Defined in:
lib/rbma/simple_ma.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n:, empty: "-") ⇒ SimpleMa

Returns a new instance of SimpleMa.



6
7
8
9
10
# File 'lib/rbma/simple_ma.rb', line 6

def initialize(n:, empty: "-")
  @n = n
  @empty = empty
  @lasts = []
end

Instance Attribute Details

#emptyObject (readonly)

Returns the value of attribute empty.



3
4
5
# File 'lib/rbma/simple_ma.rb', line 3

def empty
  @empty
end

#lastsObject (readonly)

Returns the value of attribute lasts.



4
5
6
# File 'lib/rbma/simple_ma.rb', line 4

def lasts
  @lasts
end

#nObject (readonly)

Returns the value of attribute n.



2
3
4
# File 'lib/rbma/simple_ma.rb', line 2

def n
  @n
end

Instance Method Details

#compute(current:, lasts: nil, empty: nil) ⇒ Object



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

def compute(current:, lasts: nil, empty: nil)
  if lasts.is_a?(Array)
    @lasts = lasts
    @empty = @empty ? @empty : empty
  end
  @lasts << current
  if @lasts.size > @n
    @lasts.last(5)
  end
  sum = 0
  @lasts.each{ |a| sum +=a }

  ma = (sum).to_f / @lasts.size

  if @lasts.size == @n
    ma
  else
    @empty
  end
end