# Error with regular expression inside eval block

**URL:** <https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877>\
**Category:** Ruby\
**Created:** [January 29, 2009, 12:56am UTC](https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877 "2009-01-29T00:56:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [January 29, 2009, 12:56am UTC](https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877/1 "2009-01-29T00:56:07Z")

</div>

I’m seeing strange behavior with Ruby 1.8 when I try to evaluate a  
regular expression inside an eval block.

Consider the following:

puts ‘1’ =~ /^[\d]+$/

> > outputs “0” to the terminal

eval %Q{‘1’ =~ /^[\d]+$/}

> > outputs “nil” to the screen

Could someone explain why the eval statement returns an incorrect result  
for the regexp match? Thanks…

---

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [January 29, 2009, 1:21am UTC](https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877/2 "2009-01-29T01:21:41Z")

</div>

IÃ±aki Baz C. wrote:

> El Jueves, 29 de Enero de 2009, Barun S. escribiÃ³:
> 
> > > > outputs “nil” to the screen
> > 
> > Could someone explain why the eval statement returns an incorrect result  
> > for the regexp match? Thanks…
> 
> It works if you escape the \ with \ :
> 
> eval %Q{‘1’ =~ /^[\d]+$/}  
> =\> 0
> 
> Not sure why the \ must be escaped into a block code.

Ah, thanks that worked. I wonder why extra escaping is needed just  
because it’s in a block…

---

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [January 29, 2009, 1:01am UTC](https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877/3 "2009-01-29T01:01:04Z")

</div>

El Jueves, 29 de Enero de 2009, Barun S. escribiÃ³:

> > > outputs “nil” to the screen
> 
> Could someone explain why the eval statement returns an incorrect result  
> for the regexp match? Thanks…

It works if you escape the \ with \ :

eval %Q{‘1’ =~ /^[\d]+$/}  
=\> 0

Not sure why the \ must be escaped into a block code.

---

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [January 29, 2009, 10:42am UTC](https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877/4 "2009-01-29T10:42:17Z")

</div>

Barun S. wrote:

> IÃ±aki Baz C. wrote:
> 
> > El Jueves, 29 de Enero de 2009, Barun S. escribiÃ³:
> > 
> > > > > outputs “nil” to the screen
> > > 
> > > Could someone explain why the eval statement returns an incorrect result  
> > > for the regexp match? Thanks…
> > 
> > It works if you escape the \ with \ :
> > 
> > eval %Q{‘1’ =~ /^[\d]+$/}  
> > =\> 0
> > 
> > Not sure why the \ must be escaped into a block code.
> 
> Ah, thanks that worked. I wonder why extra escaping is needed just  
> because it’s in a block…

It’s not in a block, it’s just inside a string. There are two types of  
string quoting, normally given using double-quotes and single-quotes,  
and they behave differently with regards to escaping. The double-quote  
variety converts special sequences like \n to newline, and \d just  
becomes d.

irb(main):001:0\> puts %Q{‘1’ =~ /^[\d]+$/}  
‘1’ =~ /^[d]+$/  
=\> nil

irb(main):002:0\> puts %q{‘1’ =~ /^[\d]+$/}  
‘1’ =~ /^[\d]+$/  
=\> nil

irb(main):003:0\> puts %Q{\n}

=\> nil  
irb(main):004:0\> puts %q{\n}  
\n  
=\> nil

---

<div class="post-metadata">

**Author:** ![barunio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b487fb/32.png) [@barunio](https://www.ruby-forum.com/u/barunio)\
**Post date:** [January 29, 2009, 1:59pm UTC](https://www.ruby-forum.com/t/error-with-regular-expression-inside-eval-block/158877/5 "2009-01-29T13:59:01Z")

</div>

2009/1/29 Barun S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > =\> 0
> > 
> > Not sure why the \ must be escaped into a block code.
> 
> Ah, thanks that worked. I wonder why extra escaping is needed just  
> because it’s in a block…

It’s not a block - it’s a doubly quoted string!

Ruby version 1.8.7  
irb(main):001:0\> %Q{foo}  
=\> “foo”  
irb(main):002:0\> %Q{foo}.class  
=\> String  
irb(main):003:0\> %Q{foo\no}  
=\> “foo\no”  
irb(main):004:0\> %Q{foo\no}  
=\> “foo\no”  
irb(main):005:0\> puts %Q{foo\no}  
foo  
o  
=\> nil  
irb(main):006:0\> puts %Q{foo\no}  
foo\no  
=\> nil

Kind regards

robert
