简体   繁体   English

记录并发送用户输入

[英]Recording and sending user input

Up till now, I have been my asking the user to send an integer (from choices: 1, 2, 3, 4 or 5) as the possible reply to the messages. 到目前为止,我一直在要求用户发送一个整数(从选项:1、2、3、4或5)作为对消息的可能回复。 I then give back to the user their choice of integer before moving forward to the next message. 然后,在继续下一条消息之前,我将选择的整数还给用户。 So my messages have had the following structure: 因此,我的消息具有以下结构:

Messages = [ 
{"1"=>"You chose 1.", 
"2"=>"You chose 2.", 
"message"=> "Pick 3 or 4. 3) Apples 4) Oranges."},

{"3"=>"You chose Apples.", 
"4"=>"You chose Oranges.", 
"message"=>"Pick 1 or 2. 1) [something] 2) [something]"}, 
]

I render my messages with the following call: 我通过以下调用呈现消息:

say "#{messages[$status.to_i][$reply]} #{messages[$status.to_i]['message']}"

Now I need the user to input something original like say, his/her date of birth (eg 07May90 could be a possible complete input). 现在,我需要用户输入一些原始信息,例如他/她的生日(例如,可能是90May90,可能是完整的输入)。 So I need the following to be the case. 因此,我需要满足以下条件。

Messages = [ 
{"1"=>"You chose 1.", 
"2"=>"You chose 2.", 
"message"=> "What is your date of birth?"},

{"DoB"=>"You chose [whatever user inputs e.g. 07May90].", 
"message"=>"Pick 1 or 2. 1) [something] 2) [something]"}, 
]

How can I return the original input back to the user eg if they responded to the DoB question by typing 07May90, I'd like to say "You chose 07May90." 我如何将原始输入返回给用户,例如,如果他们通过输入07May90回答了DoB问题,我想说“您选择了07May90”。 (as mentioned above). (正如刚才提到的)。

One way that springs to mind would be to use sprintf 想到的一种方法是使用sprintf

message = "You picked %s"
value = "07May90"
puts sprintf(message, value)

It's not particularly pretty mind you, but it'll work. 您介意的不是特别漂亮,但是会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM