The Accidental Rubyist

invalid byte sequence in UTF-8

Highline: ask and validate a date

leave a comment »

Here’s a snippet of code using ruby’s highline gem, that asks a user for a date, uses today’s date as a default, and validates that the date is greater than or equal to today.

due_date = ask("Enter due date? ", Date) {
|q| q.default = Date.today.to_s;
q.validate = lambda { |p| Date.parse(p) >= Date.today };
q.responses[:not_valid] = "Enter a date greater than or equal to today"
}

Advertisements

Written by totalrecall

June 28, 2010 at 4:14 pm

Posted in ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: