Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Gem::StreamUI#ask_yes_no
ask_yes_no(question, default=nil)
lib/rubygems/user_interaction.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/rubygems/user_interaction.rb, line 249
def ask_yes_no(question, default=nil)
unless tty?
if default.nil?
raise Gem::OperationNotSupportedError,
"Not connected to a tty and no default specified"
else
return default
end
end
default_answer = case default
when nil
"yn"
when true
"Yn"
else
"yN"
end
result = nil
while result.nil? do
result = case ask "#{question} [#{default_answer}]"
when /^y/i then true
when /^n/i then false
when /^$/ then default
end
end
result
end
Ask a question. Returns a true for yes, false for no. If not connected to a tty, raises an exception if default is nil, otherwise returns default.
See also
#ask_for_password
#backtrace
#ask
#choose_from_list
also in
4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0