Skip to content

Instantly share code, notes, and snippets.

@jmeirow
Created November 2, 2012 00:11
Show Gist options
  • Select an option

  • Save jmeirow/3997776 to your computer and use it in GitHub Desktop.

Select an option

Save jmeirow/3997776 to your computer and use it in GitHub Desktop.
TimeParser
class TimeParser
attr_reader :hour, :minute, :am_pm
def initialize time
matched = /(1[012]|^[1-9]):[0-5][0-5] (am|pm|AM|PM)/.match(time.lstrip.squeeze(' '))
if matched
@valid = true
parts = matched[0].split(/[\s:]/)
@hour = parts[0].to_i
@minute = parts[1].to_i
@am_pm = parts[2]
else
@valid = false
end
end
def valid?
@valid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment