@@ -48,7 +48,7 @@ def padding=(value)
48
48
def ask ( statement , *args )
49
49
options = args . last . is_a? ( Hash ) ? args . pop : { }
50
50
51
- options [ :limited_to ] ? ask_filtered ( statement , options [ :limited_to ] , *args ) : ask_simply ( statement , *args )
51
+ options [ :limited_to ] ? ask_filtered ( statement , options , *args ) : ask_simply ( statement , options , *args )
52
52
end
53
53
54
54
# Say (print) something to the user. If the sentence ends with a whitespace
@@ -372,15 +372,24 @@ def as_unicode
372
372
end
373
373
end
374
374
375
- def ask_simply ( statement , color = nil )
376
- say ( "#{ statement } " , color )
377
- stdin . gets . tap { |text | text . strip! if text }
375
+ def ask_simply ( statement , options = { } , color = nil )
376
+ default = options [ :default ]
377
+ message = [ statement , ( "(#{ default . inspect } )" if default ) , nil ] . uniq . join ( " " )
378
+ say ( message , color )
379
+ result = stdin . gets . tap { |text | text . strip! if text }
380
+
381
+ if default && result == ""
382
+ default
383
+ else
384
+ result
385
+ end
378
386
end
379
387
380
- def ask_filtered ( statement , answer_set , *args )
388
+ def ask_filtered ( statement , options = { } , *args )
389
+ answer_set = options [ :limited_to ]
381
390
correct_answer = nil
382
391
until correct_answer
383
- answer = ask_simply ( "#{ statement } #{ answer_set . inspect } " , *args )
392
+ answer = ask_simply ( "#{ statement } #{ answer_set . inspect } " , options , *args )
384
393
correct_answer = answer_set . include? ( answer ) ? answer : nil
385
394
answers = answer_set . map ( &:inspect ) . join ( ", " )
386
395
say ( "Your response must be one of: [#{ answers } ]. Please try again." ) unless correct_answer
0 commit comments