Skip to content

Commit 0df033c

Browse files
committed
Merge pull request #2 from weakish/ask-default-answer
Add support for default answer to ask.
2 parents 72e312c + 430b74d commit 0df033c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ line options parser code.
6969
In addition to #cli, CLI.K provides the #ask method. This is a very simple
7070
command line query method.
7171

72-
ans = ask "Are you nice? [Y/N]"
72+
ans = ask "Are you nice? [Y/n]"
7373

7474
Other Ruby libraries have their own take on the #ask method, and this very
7575
simple implementation can just as soon be overridden. No biggy. But it's nice

lib/clik/ask.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Kernel
66
# via the console. A prompt will be sent to $stdout,
77
# if given, and the input taken from $stdin...
88
#
9-
# ask "Are you happy? [Yn]"
9+
# ask "Are you happy? [Yn]", "Y"
1010
#
1111
# On the command line one would see...
1212
#
@@ -19,11 +19,15 @@ module Kernel
1919
# The ask method would return "Y".
2020
#
2121
# Returns [String]
22-
def ask(prompt=nil)
22+
def ask(prompt=nil, default_answer=nil)
2323
$stdout << "#{prompt}"
2424
$stdout.flush
25-
$stdin.gets.chomp!
25+
ans = $stdin.gets.chomp!
26+
if ans == ''
27+
default_answer
28+
else
29+
ans
30+
end
2631
end
27-
2832
end
2933

0 commit comments

Comments
 (0)