Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Created August 29, 2018 11:27
Show Gist options
  • Save matthewrudy/aea43c557585197005eddd493647433e to your computer and use it in GitHub Desktop.
Save matthewrudy/aea43c557585197005eddd493647433e to your computer and use it in GitHub Desktop.
A way of counting multiple values at once

MultiCounter

Usage:

counter = MultiCounter.new(3)

counter.add(12,2,10)
counter.add(9,9,4)

counter.values
# => [21, 11, 14]
class MultiCounter
def initialize(n)
@counters = [0]*n
end
def add(*values)
values.each_with_index do |v, i|
@counters[i] += v
end
end
def values
@counters
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment