File: sum.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (34 lines) | stat: -rw-r--r-- 702 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

module TTFunk
  # Sum aggreaget. Is sums all pushed values.
  class Sum < Aggregate
    # Value
    #
    # @return [#+]
    attr_reader :value

    # @param init_value [#+] initial value
    def initialize(init_value = 0)
      super()
      @value = init_value
    end

    # Push a value. It will be added to the current value.
    #
    # @param operand [any]
    # @return [void]
    def <<(operand)
      @value += coerce(operand)
    end

    # Get the stored value or default.
    #
    # @param _default [any] Unused. Here for API compatibility.
    # @return [any]
    def value_or(_default)
      # value should always be non-nil
      value
    end
  end
end