From: "Sean E. McCardell" Date: 2005-04-29T07:01:15+09:00 Subject: Re: [SOLUTION] HighLine (#29) [LONG] On 11:52 Thu 28 Apr , James Edward Gray II wrote: > Any chance you could give us a few simple examples of usage? For > example, how do the quiz examples translate to this system? Sure thing. Here goes: require 'highline' # This might be useful for someone implementing COMMAND.COM in Ruby :) class DiskError < HighLine::ChoiceInput choices "abort", "retry", "fail" synonym "abort", "a" synonym "retry", "r" synonym "fail", "f" end result = DiskError.ask("Error reading drive A:") # And the output will look like: # Error reading drive A: [abort/retry/fail] # The user will continue to be prompted until "abort", "retry", or # "fail" is entered (or one of their synonyms, "a", "r", or "f" # For the age example from the quiz, I would do: class IntegerInput < HighLine::ValueInput validate /^\d+$/ # when a validation procedure returns a three-element array, # the second element can be an error message, and the third # element will be used as an alternate test value (instead of the # user's response string) for subsequent validation tests. validate proc { |r| [true, nil, r.to_i] } end age = IntegerInput.ask("Enter your age:") { validate :between?, 0, 105 }.alternate # The #alternate method of the returned object gives you access to # the alternate test value, if any, created during validation. In this # case, it is an Integer # And for an indirect way of finding an age, here's one that demonstrates # using an instance method for validation: require 'date' class DateInput < HighLine::ValueInput validate :with_my, :ensure_date def ensure_date(response) begin test_date = Date.parse(response) rescue ArgumentError false else [true, nil, test_date] end end end birthday = DateInput.ask("When were you born?") { # output_format, like validate, operates on an alternate test value # if one was created during validation. This just calls #to_s on that # value, so you always get anwers in the form "YYYY-MM-DD", even if # you enter something like "April 28th, 2005" output_format :to_s error_message "Please enter a valid date" validate proc { |r| [r <= Date.today, "You can't be from the future!"] } } Hope this helps, --Sean