Take the 2-minute tour ×

Using Rails running on Heroku, I've encountered a situation where a regular expression match is found in a string but the $1 variable is not being set for the group in parenthesis. This happens only in my production setup on Heroku, and not on my local system (Rails 3.0.7 with Ruby ruby-1.9.2-p0). The code is

puts "**** gateway_reply=#{gateway_reply}"
match = gateway_reply =~ /ID: (\w+)/
msg_id = $1
puts "**** match=#{match}, msg_id ($1) = #{$1}"

On my local system, the output is

**** gateway_reply=ID: da2x7s5tjumtxtnk1krl8wps4wpasiee
**** match=0, msg_id ($1) = da2x7s5tjumtxtnk1krl8wps4wpasiee

On the production system, $1 is not set:

**** gateway_reply=ID: 93e4ca3590207761af6f3b5ba3545b36

Any answers as to what is going on here?

share|improve this question
What stack are you using on heroku? Are you sure it matches your development environment? –  aubreyrhodes Jun 22 '12 at 20:24
You may want to use \s+ instead of the space in your regex. A space can be many different values based on environment –  Michael Papile Jun 22 '12 at 20:33
That's a good idea, Michael, though since the expression matches it should still return a value in $1, shouldn't it? Aubrey, the stack is aspen-mri-1.8.6, * bamboo-mri-1.9.2, bamboo-ree-1.8.7, cedar. Does some version of Ruby have different behavior for return values of matches? –  Mike Blyth Jun 22 '12 at 21:12

1 Answer 1

works for me:

> gateway_reply='ID: da2x7s5tjumtxtnk1krl8wps4wpasiee'
> gateway_reply.match(/ID: (\w+)/)
> $1
=> "da2x7s5tjumtxtnk1krl8wps4wpasiee"


> gateway_reply =~ /ID: (\w+)/
=> 0
> $1
=> "da2x7s5tjumtxtnk1krl8wps4wpasiee"

please note that the result of the match operator ~ is the position of the match

share|improve this answer

Your Answer


