From: Brian Candler Date: 2009-01-29T18:39:49+09:00 Subject: Re: error with regular expression inside eval block Barun Singh wrote: > I単aki Baz Castillo wrote: >> El Jueves, 29 de Enero de 2009, Barun Singh 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 -- Posted via http://www.ruby-forum.com/.