我正在尝试查看是否可以在此程序中创建一个带有哈希的用户输入请求列表。什么是最好的解决方法?
print "would you like to begin a changeover? (y/n) "
input = gets.chomp
tracking = {
"start time" => input.Time.now,
"Type?" => input,
"shift?" => input,
"standard hrs?" => input,
"actual hrs?" => input,
"end time" => input = gets.chomp.Time.now
}
tracking.each do |key, value|
puts "great please answer the following: #{tracking}"
end
提前感谢!
答案 0 :(得分:0)
你必须记住评估是顺序的,从上到下(除非你定义函数/方法)。在您的代码中:
y
input
)
y
,其中两个将包含当前时间或者至少如果gets.chomp.Time.now
不是错误的话。
所以,要注意时间:
print "would you like to begin a changeover? (y/n) "
unless gets.chomp.tolower == 'n'
puts "great please answer the following:"
tracking = {
"start time" => Time.now
}
[
"Type?",
"shift?",
"standard hrs?",
"actual hrs?"
].each do |question|
print question
tracking[question] = gets.chomp
}
tracking["end_time"] = Time.now
end
答案 1 :(得分:0)
非常感谢!这让我走上正轨。但是,没有按照我想要的方式给问卷的开头和结尾加上时间戳。然而,在我自己玩了一些代码后,我能够使它工作。
print "would you like to begin a changeover? (y/n) "
unless gets.chomp. == "n"
puts Time.now
把“很好,请回答以下问题:”
端
questionnaire = ["Type", "Shift?", "Standard hrs?", "Actual hrs?"]
.each do |question|
puts question
questionnaire = gets.chomp
end
将“完成更改后请输入'完成'” input = gets.chomp 放下Time.now