Class: Accumulators::Sum
- Inherits:
-
Object
- Object
- Accumulators::Sum
- Defined in:
- lib/accumulators/sum.rb
Instance Attribute Summary collapse
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
Instance Method Summary collapse
- #add(rhs) ⇒ Object
-
#initialize ⇒ Sum
constructor
A new instance of Sum.
Constructor Details
#initialize ⇒ Sum
Returns a new instance of Sum.
5 6 7 |
# File 'lib/accumulators/sum.rb', line 5 def initialize @sum = 0 end |
Instance Attribute Details
#sum ⇒ Object (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 |