Class: CorrectHorseBatteryStaple::RangeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/correct_horse_battery_staple/range_parser.rb

Overview

doesn’t handle X…Y

Constant Summary collapse

NUM =
'-?(?:\.[0-9]+|[0-9]+|[0-9]+\.[0-9]+|[0-9]+\.(?!\.))'
SPACE =
" *"
SEPARATOR =
"(-|\\.\\.)"
REGEX_PAIR =
Regexp.new("(#{NUM})#{SPACE}#{SEPARATOR}#{SPACE}(#{NUM})")
REGEX_SINGLE =
Regexp.new("#{SPACE}(#{NUM})#{SPACE}")

Instance Method Summary collapse

Instance Method Details

#parse(string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/correct_horse_battery_staple/range_parser.rb', line 9

def parse(string)
  match = string.match(REGEX_PAIR)
  if match
    return Range.new(parse_number(match[1]), parse_number(match[3]))
  end

  match = string.match(REGEX_SINGLE)
  if match
    num = parse_number(match[0])
    return Range.new(num, num)
  end

  nil
end