error with regular expression inside eval block

Discussion in 'Ruby' started by Barun Singh, Jan 28, 2009.

  1. Barun Singh

    Barun Singh Guest

    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...
    --
    Posted via http://www.ruby-forum.com/.
     
    Barun Singh, Jan 28, 2009
    #1
    1. Advertising

  2. El Jueves, 29 de Enero de 2009, Barun Singh escribi=C3=B3:
    > 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' =3D~ /^[\d]+$/
    >
    > >> outputs "0" to the terminal

    >
    > eval %Q{'1' =3D~ /^[\d]+$/}
    >
    > >> 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' =3D~ /^[\\d]+$/}
    =3D> 0

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

    =2D-=20
    I=C3=B1aki Baz Castillo
     
    Iñaki Baz Castillo, Jan 28, 2009
    #2
    1. Advertising

  3. Barun Singh

    Barun Singh Guest

    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...
    --
    Posted via http://www.ruby-forum.com/.
     
    Barun Singh, Jan 29, 2009
    #3
  4. 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/.
     
    Brian Candler, Jan 29, 2009
    #4
  5. 2009/1/29 Barun Singh <>:
    > I=F1aki Baz Castillo wrote:
    >> El Jueves, 29 de Enero de 2009, Barun Singh escribi=F3:
    >>>
    >>> >> outputs "nil" to the screen
    >>>
    >>> Could someone explain why the eval statement returns an incorrect resul=

    t
    >>> for the regexp match? Thanks...

    >>
    >> It works if you escape the \ with \\ :
    >>
    >> eval %Q{'1' =3D~ /^[\\d]+$/}
    >> =3D> 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}
    =3D> "foo"
    irb(main):002:0> %Q{foo}.class
    =3D> String
    irb(main):003:0> %Q{foo\\no}
    =3D> "foo\\no"
    irb(main):004:0> %Q{foo\no}
    =3D> "foo\no"
    irb(main):005:0> puts %Q{foo\no}
    foo
    o
    =3D> nil
    irb(main):006:0> puts %Q{foo\\no}
    foo\no
    =3D> nil

    Kind regards

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
     
    Robert Klemme, Jan 29, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,336
  2. morrell
    Replies:
    1
    Views:
    977
    roy axenov
    Oct 10, 2006
  3. Steve V
    Replies:
    6
    Views:
    249
    Steve V
    Apr 20, 2005
  4. David Liang

    Catch regular expression errors with eval

    David Liang, Sep 21, 2009, in forum: Perl Misc
    Replies:
    1
    Views:
    117
    Uri Guttman
    Sep 21, 2009
  5. graphicsxp

    issue with eval and regular expression

    graphicsxp, Jun 11, 2009, in forum: Javascript
    Replies:
    7
    Views:
    159
    Evertjan.
    Jun 13, 2009
Loading...

Share This Page