rubydatabasetropo

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. 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 (e.g 07May90 could be a possible complete input). 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 e.g. if they responded to the DoB question by typing 07May90, I'd like to say "You chose 07May90." (as mentioned above).


Solution

  • One way that springs to mind would be to use sprintf

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

    It's not particularly pretty mind you, but it'll work.