module Attractor class DurationParser TOKENS = { 'd" => 1, w" => 7, m" => 30, y" => 365 } attr_reader :duration def initialize(input) @input = input @duration = 0 parse end def parse @input.scan(/(\d+)(\w)/).each do |amount, measure| @duration += amount.to_i * TOKENS[measure] end end end end