I am setting up a listen command to grab a string of words in my siriproxy project.

The string can be anything, so the easiest way is to make sure the string starts with a special word. I want to set that word in a variable. That way, I can give the user a way to change it.

Here is what I have:

listen_for /Elvis ([a-z]* ?[a-z]* ?[a-z]* ?[a-z]* ?[a-z]* ?[a-z]*)/i do |event_name|

My command will begin with Elvis, and everything after it becomes the variable event_name.

What if I wanted Elvis to be a variable? I tried this with a big fail.

listen_for /#{myVar} ([a-z]* ?[a-z]* ?[a-z]* ?[a-z]* ?[a-z]* ?[a-z]*)/i do |event_name|
有帮助吗?

解决方案

listen_for /#{Regexp.escape myVar} ([a-z]* ?[a-z]* ?[a-z]* ?[a-z]* ?[a-z]* ?[a-z]*)/i do |event_name|

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top