HighLine:使用gather和answer_or_default

时间:2016-01-26 17:28:09

标签: ruby highline

这是我目前的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行,姓名,昵称和电子邮件

在散列或数组中,但我相信散列会更容易阅读。

1 个答案:

答案 0 :(得分: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。