From 2a70160ab98d55c30ba9f9c742a61eb3ef143dbb Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Fri, 19 Aug 2022 01:26:06 +0900 Subject: [PATCH] Refactor WebMock::Util::HashCounter --- lib/webmock/util/hash_counter.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/webmock/util/hash_counter.rb b/lib/webmock/util/hash_counter.rb index 6e9e17538..77f48db34 100644 --- a/lib/webmock/util/hash_counter.rb +++ b/lib/webmock/util/hash_counter.rb @@ -4,26 +4,30 @@ module WebMock module Util class HashCounter attr_accessor :hash + def initialize - self.hash = {} + self.hash = Hash.new(0) @order = {} @max = 0 @lock = ::Mutex.new end - def put key, num=1 + + def put(key, num=1) @lock.synchronize do - hash[key] = (hash[key] || 0) + num - @order[key] = @max = @max + 1 + hash[key] += num + @order[key] = @max += 1 end end - def get key + + def get(key) @lock.synchronize do - hash[key] || 0 + hash[key] end end def select(&block) return unless block_given? + @lock.synchronize do hash.select(&block) end