Irb regexp strangeness

I’ve noticed the following behaviour when playing with irb:

martin@beyond ruby $ cat t.rb
puts ‘testing’ =~ /test/
puts ‘testing’ =~ 'test’
puts ‘testing’ !~ /test/
puts ‘testing’ !~ 'test’
martin@beyond ruby $ ruby t.rb
0
t.rb:2: warning: string =~ string will be obsolete; use explicit regexp
0
false
t.rb:4: warning: string =~ string will be obsolete; use explicit regexp
false

Fine as a script, however:

martin@beyond ruby $ irb
irb(main):001:0> ‘testing’ =~ /test/
=> 0
irb(main):002:0> ‘testing’ =~ ‘test’
(irb):2: warning: string =~ string will be obsolete; use explicit regexp
=> 0
irb(main):003:0> ‘testing’ !~ /test/
irb(main):004:0* ^C <-----IRB gets stuck here
irb(main):004:0> ‘testing’ !~ ‘test’
(irb):4: warning: string =~ string will be obsolete; use explicit regexp
=> false
irb(main):005:0>

Why the hang on line 4?

cheers,

Martin

Why the hang on line 4?

You must have the same problem with !=, no ?

Guy Decoux

Why the hang on line 4?

You must have the same problem with !=, no ?

Yes,

irb(main):010:0> ‘testing’ != /test/
irb(main):011:0*

Found this in a previous ruby-talk message:

    > 
    > Hi,
    > 
    >  Searching through "Programming Ruby" I found the Perl-like
    "=~" for
    >  matching strings against regexps but I cannot find the
    opposite
    >  "dont match" operator ( "!~" in Perl ).
    > 
    >  Is there any aequivalent ?
    
    (Actually, a !~ b is the same as !(a =~ b), i.e. there is no !~
    method as
    such; in the same way that a += b is expanded to a = a + b and
    so there is
    no += operator as such)

So this expansion only works with a String on the rhs?

regards,

Martin

···

On Sat, 2003-08-23 at 01:02, ts wrote:
On Sun, Aug 17, 2003 at 07:46:33PM +0900, Meino Christian Cramer wrote:

So this expansion only works with a String on the rhs?

no, probably a bug in irb (ruby-lex.rb)

Guy Decoux

In [ruby-talk:79947] the message: “irb regexp strangeness”, on Aug/23
00:31(JST) ts writes:

So this expansion only works with a String on the rhs?

no, probably a bug in irb (ruby-lex.rb)

I fixed this bug.

Thanks for bug reports.

– cut here–
diff -u -r1.20 -r1.21
— lib/irb/ruby-lex.rb 5 Aug 2003 03:08:15 -0000 1.20
+++ lib/irb/ruby-lex.rb 23 Aug 2003 07:51:19 -0000 1.21
@@ -1,8 +1,8 @@

···

irb/ruby-lex.rb - ruby lexcal analizer

$Release Version: 0.9$

-# $Revision: 1.20 $
-# $Date: 2003/08/05 03:08:15 $
+# $Revision: 1.21 $
+# $Date: 2003/08/23 07:51:19 $

by Keiju ISHITSUKA(keiju@ishitsuka.com)

@@ -15,7 +15,7 @@
require “irb/ruby-token”

class RubyLex

  • @RCS_ID=‘-$Id: ruby-lex.rb,v 1.20 2003/08/05 03:08:15 keiju Exp $-’
  • @RCS_ID=‘-$Id: ruby-lex.rb,v 1.21 2003/08/23 07:51:19 keiju Exp $-’

    extend Exception2MessageMapper
    def_exception(:AlreadyDefinedToken, “Already defined token(%s)”)
    @@ -392,7 +392,7 @@

    @OP.def_rules(“!”, “!=”, “!~”) do
    >op, io|

  •  #@lex_state = EXPR_BEG
    
  •  @lex_state = EXPR_BEG
     Token(op)
    
    end

– keiju