diff --git a/README.md b/README.md index 58fa344..2595db5 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ line options parser code. In addition to #cli, CLI.K provides the #ask method. This is a very simple command line query method. - ans = ask "Are you nice? [Y/N]" + ans = ask "Are you nice? [Y/n]" Other Ruby libraries have their own take on the #ask method, and this very simple implementation can just as soon be overridden. No biggy. But it's nice diff --git a/lib/clik/ask.rb b/lib/clik/ask.rb index 0da4808..c61770b 100644 --- a/lib/clik/ask.rb +++ b/lib/clik/ask.rb @@ -6,7 +6,7 @@ module Kernel # via the console. A prompt will be sent to $stdout, # if given, and the input taken from $stdin... # - # ask "Are you happy? [Yn]" + # ask "Are you happy? [Yn]", "Y" # # On the command line one would see... # @@ -19,11 +19,15 @@ module Kernel # The ask method would return "Y". # # Returns [String] - def ask(prompt=nil) + def ask(prompt=nil, default_answer=nil) $stdout << "#{prompt}" $stdout.flush - $stdin.gets.chomp! + ans = $stdin.gets.chomp! + if ans == '' + default_answer + else + ans + end end - end