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