0

这是我当前用于 ircd 配置文件生成器的代码,我试图能够使用answer_or_default它,并弄清楚使用HighLine::Question#gather

这是answer_or_default

a = HighLine.new($stdin, $stderr)
connect_pingfreq = a.ask("? ", Integer) do |q|
  q.default = 240
  q.in = 0..300
  # q.answer_or_default
  # gives
  # /var/lib/gems/2.0.0/gems/highline
  # -1.7.8/lib/highline/question.rb:217:in
  # `answer_or_default': wrong number of arguments (0 for 1) (ArgumentError)
end
# I've used it here as
# connect_pingfreq.answer_or_default
# But then gives the error shown
# ? 
# /home/ken/git/shell-scripts/inspigen/generator.rb:48:in `conf': 
# undefined method `answer_or_default' for 240:Fixnum (NoMethodError)

在内部使用 answer_or_default 时,我不太确定如何为其提供文档所说的“answer_string”。

然后 240 是给出的答案,但我无能为力。

我还想学习HighLine::Question#gather使用这段代码的用法。考虑到我不太了解它的文档

a = HighLine.new($stdin, $stderr)
a.say "Your <admin> lines.."
admin_name = a.ask "Admin Real Name? "
admin_nick = a.ask "Admin Nick? "
admin_email = a.ask("Admin Email? ") do |q|
  q.validate = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/
end

#gather 应该向我询问 3 行、姓名、昵称和电子邮件

无论是散列还是数组,但我相信散列会更容易阅读。

4

1 回答 1

0

对于您的第一个问题,您可以从connect_pingfreq变量中获得答案(或默认值)。无需调用answer_or_default。像这样:

a = HighLine.new($stdin, $stderr)
connect_pingfreq = a.ask("? ", Integer) do |q|
  q.default = 240
  q.in = 0..300
end

puts connect_pingfreq

这将输出用户输入的答案或默认值 240。

于 2016-01-26T18:59:09.337 回答