Issue #13135 has been updated by Shugo Maeda.


Shugo Maeda wrote:
> Regexp.last_match returns nil, if // is given to String#rindex:

The following patch seems to fix it:

```diff
diff --git a/string.c b/string.c
index 919a46d..b5aae67 100644
--- a/string.c
+++ b/string.c
@@ -3516,10 +3516,8 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str)
 	pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos,
 			 enc, single_byte_optimizable(str));
 
-	if (!RREGEXP_PTR(sub) || RREGEXP_SRC_LEN(sub)) {
-	    pos = rb_reg_search(sub, str, pos, 1);
-	    pos = rb_str_sublen(str, pos);
-	}
+	pos = rb_reg_search(sub, str, pos, 1);
+	pos = rb_str_sublen(str, pos);
 	if (pos >= 0) return LONG2NUM(pos);
 	break;
 
```


----------------------------------------
Bug #13135: Regexp.last_match returns nil with s.rindex(//)
https://bugs.ruby-lang.org/issues/13135#change-62523

* Author: Shugo Maeda
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Regexp.last_match returns nil, if // is given to String#rindex:

```
lexington:ruby$ ruby -ve 'p "foo".rindex(//); p Regexp.last_match'
ruby 2.5.0dev (2017-01-17 trunk 57359) [x86_64-linux]
3
nil
```

while it returns a MatchData with String#index:

```
lexington:ruby$ ruby -ve 'p "foo".index(//); p Regexp.last_match' 
ruby 2.5.0dev (2017-01-17 trunk 57359) [x86_64-linux]
0
#<MatchData "">
```

Is this intended behavior?




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>