Either initialize the variables in your class's constructor (that's specifically what it's for!) or define accessor methods to isolate the initialization logic for each variable:
# via constructor: def initialize @num_attempts = 0 @all_attempts = 0 end def run_counters num_attempts += retry_attempt all_attempts += retry_limit end
Or:
# via accessor methods: def run_counters num_attempts += retry_attempt all_attempts += retry_limit end def num_attempts @num_attempts ||= 0 end def all_attempts @all_attempts ||= 0 end
This also means you may safely access num_attempts
and all_attempts
from any other method, and not have to duplicate the initialization logic.