[Q] Change in 1.8 String#=~ intentional?

In converting some code from Ruby version 1.6.8 to 1.8, I found that the
construct:

“x” =~ “[x]”

has apparently changed behavior.  In version 1.6.8, String#=~ will

convert a String argument into a Regexp. In version 1.8, apparently this is
now translated into a call to String#index. This can lead to results like:

%ruby -ve "p ‘x^x’ =~ ‘^x’"
ruby 1.6.8 (2002-12-24) [i386-freebsd4.7]
0

%ruby -ve "p ‘x^x’ =~ ‘^x’"
ruby 1.8.0 (2003-05-01) [i386-freebsd4.7]
1

Is this change intentional?  It would seem that this change would affect

a lot of exsiting code in subtle, hard-to-find ways :o(

- Warren Brown

Hi,

···

At Sat, 24 May 2003 07:07:45 +0900, Warren Brown wrote:

Is this change intentional?  It would seem that this change would affect

a lot of exsiting code in subtle, hard-to-find ways :o(

Yes. But I think it should be warned now.

Index: string.c

RCS file: /cvs/ruby/src/ruby/string.c,v
retrieving revision 1.155
diff -u -2 -p -r1.155 string.c
— string.c 19 May 2003 07:11:48 -0000 1.155
+++ string.c 24 May 2003 00:43:36 -0000
@@ -1028,4 +1028,12 @@ rb_str_match(x, y)

   case T_STRING:

+#if RUBY_VERSION_CODE < 181

  • {
  •   VALUE r = rb_reg_quote(y);
    
  •   if (r != y && rb_str_cmp(r, y) != 0) {
    
  •   rb_warn("usage of String#=~ to String is deprecated; use Regexp#=~ or String#index instead");
    
  •   }
    
  • }
    +#endif
    start = rb_str_index(x, y, 0);
    if (start == -1) {
    @@ -1460,5 +1468,5 @@ get_pat(pat, quote)
    if (quote) {
    val = rb_reg_quote(pat);
    -#if RUBY_VERSION_CODE < 180
    +#if RUBY_VERSION_CODE < 181
    if (val != pat && rb_str_cmp(val, pat) != 0) {
    rb_warn(“string pattern instead of regexp; metacharacters no longer effective”);


Nobu Nakada

Hi,

In converting some code from Ruby version 1.6.8 to 1.8, I found that the
construct:

“x” =~ “

has apparently changed behavior. In version 1.6.8, String#=~ will
convert a String argument into a Regexp. In version 1.8, apparently this is
now translated into a call to String#index.

Is this change intentional? It would seem that this change would affect
a lot of exsiting code in subtle, hard-to-find ways :o(

This is now under discussion in ruby-list. The behavior will be
either

  • use String#index internally, with warning.
  • give warning to discourage using string as an argument.

I will have warning in 1.8.0 anyway for any incompatible changes,
at least I’m trying so.

						matz.
···

In message “[Q] Change in 1.8 String#=~ intentional?” on 03/05/24, “Warren Brown” wkb@airmail.net writes: