Parsing entries – regular expressions
timelog should understand lines like this:
2026-09-15 08:30-12:00 ProjectX planning-meetingThat's what regular expressions (regex) are for: patterns that describe text. \d is a digit, {2} means “exactly two of them”, and (?<name>…) gives a match a name:
line = "2026-09-15 08:30-12:00 ProjectX planning-meeting"
pattern = /(?<date>\d{4}-\d{2}-\d{2}) (?<from>\d{2}:\d{2})-(?<to>\d{2}:\d{2}) (?<project>\S+)/
hit = line.match(pattern)
hit[:project]match returns a MatchData object – you fetch named groups with hit[:from]. If nothing matches you get nil (perfect for if). Quick test without data: line.match?(pattern).
For branching, Ruby has the elegant case/when – it understands ranges, classes and even regexes:
def classify(hours)
case hours
when 0...4 then "half day"
when 4...9 then "full day"
else "overtime!"
end
end
classify(7.5)Task: Write
parse_line(line): it splits a timelog line using named groups and returns { project:, from:, to: } – or nil for lines that don't match. Test it as the last line: parse_line("2026-09-15 08:30-12:00 ProjectX meeting").# def parse_line(line)
# pattern = /.../ # named groups: date, from, to, project
# ...
# end