Forum: Ruby error with regular expression inside eval block

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A70410682bd132d5edf76b42189ebb45?d=identicon&s=25 Barun Singh (barunio)
on 2009-01-29 00:56
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...
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-01-29 01:01
(Received via mailing list)
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.
A70410682bd132d5edf76b42189ebb45?d=identicon&s=25 Barun Singh (barunio)
on 2009-01-29 01:21
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...
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-29 10:42
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-29 13:59
(Received via mailing list)
2009/1/29 Barun Singh <barunio@gmail.com>:
>>   => 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
This topic is locked and can not be replied to.