Ruby $ Variables

Catalog of dial plans
Post Reply
azrobert
Posts: 55
Joined: Sat Oct 24, 2009 11:37 pm

Ruby $ Variables

Post by azrobert » Tue Dec 08, 2009 12:33 am

Thru trial and error and some web searches I learned how to use the Ruby $ variables and thought I would share it with the forum. If I state something wrong please correct me. I hope my terminology is correct.

In the below example I’m comparing for a single digit followed by an “*” and then 10 more digits.

nbr=”1*4805551212”
If nbr .match /^\d\*\d{10}$/

If you place parentheses around each segment of the compare you create 3 groups like this:

nbr=”1*4805551212”
If nbr .match /^(\d)(\*)(\d{10}$)/

If you get a match the data that satisfies each group will end up in a corresponding $ variable. So in the above example $1 will equal “1”, $2 will equal “*” and $3 will equal “4805551212”.

If you do this:

nbr=”1*4805551212”
If nbr .match /^(\d)\*(\d{10}$)/

You only create only 2 groups, so $1 will equal “1” and $2 will equal “4805551212”.

You can use the above example to route calls to 10 providers using one compare like this:

Name your providers “provider0” thru “provider9”. Then:

if nbr.match /^(\d)\*( \d{10}$)/
sys.Dial("#{$2}@provider#{$1}")
end


I know this is simple for the more advanced programmers, but I hope the above is some use to the Ruby challenged out there like myself.

Happy coding.

Post Reply