1

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).

msc
  • 95
  • 7
  • Well, you will have to improve on your request processing and figure out a way (e.g. through a RegEx) to determine that the user input a date of birth. As you are not showing us the code you use to process your replies this becomes awfully hard to answer. It is really hard to give a suggestion if we just see your `Messages` hash. – Patru Jul 16 '14 at 00:44
  • I'm not sure how to improve upon request processing. That is exactly my question. If you would like to look into it, I have edited my question to add the code used for processing replies and updating the database. I hope that makes it easy to give a workable suggestion. – msc Jul 16 '14 at 01:51
  • I think your `updateCouchDBData` method reveals part of the problem. You try to do too many things at once. This confuses things until they are almost not recognizable anymore. First you should separate your session finding code from the rest, put it in a method like `find_session` and proceed only if you find a session. Then you can continue to interpret your `extra` parameter which I *guess* is the `$reply` you mention in the code above. Frankly speaking, the two parts seem to have little to do with each other at the moment. – Patru Jul 16 '14 at 06:40

1 Answers1

0

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.

Some Guy
  • 1,481
  • 12
  • 26
  • Thanks for pointing that out. I'm not sure where the puts sprintf(message, value) statement would go since I am not printing the message anywhere; it needs to be sent to the user, so it needs to be the value of the messages hash, but since that value is usually a string, I am not sure how to implement your suggestion. – msc Jul 16 '14 at 02:20
  • sprintf returns a string. You don't have to pass it to puts, you can send it wherever you want. – Some Guy Jul 16 '14 at 02:22
  • How do you imagine your suggestion working out? What comes to mind is this: Messages = [ {"1"=>"You chose 1.", "2"=>"You chose 2.", "message"=>"Please enter your bus number."}, {"value" => "Your date of birth is %s", "message" => "Thanks for your responses."}, ] Where and how will the puts sprintf statement be incorporated into this? – msc Jul 16 '14 at 02:29
  • You mention this "#{messages[$status.to_i][$reply]} #{messages[$status.to_i]['message']}". Are you aware that you can put *any* valid ruby expression inside of #{}? Such as "#{sprintf(message, whatever_the_user_entered)}" – Some Guy Jul 16 '14 at 02:37
  • I can try that. However, how would I know whatever the user enters? – msc Jul 16 '14 at 02:42
  • how do you know now? You can't pick between "You chose 1" and "You chose 2" without already knowing which one they picked. – Some Guy Jul 16 '14 at 02:43
  • 1
    In that case, I knew that the only two options were 1 or 2. So I could hardcode my responses to two options. But with something like the DoB, the possibilities are too many. Do you reckon I could use something like this: {"value" => "Your date of birth is %s", "message" => "Thanks for your responses."}, ] in my Messages array? – msc Jul 16 '14 at 02:50
  • It does not seem to work completely yet. Following your suggestion, I made the following changes. Added this to the end of my Messages array: {"value" => "Your bus number is %s", "message" => "Thank you for your responses! Have a great day."}, Rendered the above using: if $status == 6 say "#{sprintf(message, value)}" say "#{messages[$status.to_i]['message']}" end The app does not send back the value, and considers anything entered for the last message a wrong choice. – msc Jul 16 '14 at 03:33
  • Please let me know if you have any suggestion. – msc Jul 16 '14 at 04:09