Class: Accumulators::Sum

Inherits:
Object
  • Object
show all
Defined in:
lib/accumulators/sum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSum

Returns a new instance of Sum.



5
6
7
# File 'lib/accumulators/sum.rb', line 5

def initialize
  @sum = 0
end

Instance Attribute Details

#sumObject (readonly)

Returns the value of attribute sum.



3
4
5
# File 'lib/accumulators/sum.rb', line 3

def sum
  @sum
end

Instance Method Details

#add(rhs) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/accumulators/sum.rb', line 9

def add(rhs)
  if rhs.is_a? Numeric
    @sum += rhs
  elsif rhs.is_a? self.class
    @sum += rhs.sum
  else
    raise ArgumentError.new("You may not add #{rhs.class} to #{self.class}")
  end
end