[ruby-talk:00406] Re: Ruby regular expression incompatibility/bug

From: Clemens Hintze <cle@...>
Date: 1999-06-29 17:43:04 UTC
List: ruby-talk #406
Hello Julian,

unfortunately you are cheated by the obvious similary syntax of Ruby
and Perl. Perls:

$foo = '"'
$foo =~ s/"/x/           # works as expected
print "$foo\n"

you must write in Ruby as:

foo = '"'
foo.sub!(/\"/,"x")
print "#{foo}\n"

mean, isn't it? ;-)

In Perl /.../ is only syntactic sugar for m/.../. That will compile
CODE to match a certain regexp against $_ or any other variable.

But in Ruby /.../ will really instantiate an object of the Regexp
class.  The operator "=~" will await a Regexp instance on the right
side, and then match it against the left side. Other than in Perl,
there is no real matching code introduced behind your back by "=~",
only a method invokation.

So /abc/ means really: Regexp::new("abc"). And

   "hello" =~ /ll/

really means:

   "hello".match(Regexp::new("ll"))

Whereby there is no method "match" in class String (BTW: Why not
matz?). On C level there is "rb_str_match" and "rb_str_match2"! But on
Ruby level there is only "~" and "=~".

That is likely the same as 1..9, which really means Range::new(1, 9).

HTH,
Clemens.

On Tue, 29 Jun 1999, you wrote:
>Using ruby 1.3.4-990625 [i586-linux],
>
>Perl:
>% perl
>$foo = '"'
>$foo =~ s/"/x/           # works as expected
>print "$foo\n"
>--> x
>
>Ruby:
>% ruby
>foo = '"'
>foo =~ s/"/x/
>print "#{foo}\n"
>--> -:3: undefined local variable or method `s' for #<Object:0x4012ced4>
>---> (NameError)

[...]

>Thank you,

In This Thread