Skip to content


Subversion checkout URL

You can clone with
Download ZIP


String slice #1342

merged 2 commits into from

2 participants


Fixed failing specs around String.slice when regexp and group name are passed.

@rue rue merged commit df45c82 into rubinius:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 23, 2011
  1. @wrozka

    string#slice fixed for a regexp,string arguments

    Pawel Pierzchala authored wrozka committed
  2. @wrozka
This page is out of date. Refresh to see the latest.
22 kernel/common/string.rb
@@ -209,13 +209,12 @@ def =~(pattern)
# a["bye"] #=> nil
def [](index, other = undefined)
unless other.equal?(undefined)
- length = Rubinius::Type.coerce_to(other, Fixnum, :to_int)
if index.kind_of? Regexp
- match, str = subpattern(index, length)
+ match, str = subpattern(index, other)
Regexp.last_match = match
return str
+ length = Rubinius::Type.coerce_to(other, Fixnum, :to_int)
start = Rubinius::Type.coerce_to(index, Fixnum, :to_int)
return substring(start, length)
@@ -2095,23 +2094,20 @@ def modify!
def subpattern(pattern, capture)
- # TODO: A part of the functionality here should go into MatchData#[]
match = pattern.match(self)
- if !match or capture >= match.size
- return nil
- end
- if capture < 0
- capture += match.size
- return nil if capture <= 0
+ return nil unless match
+ if index = Rubinius::Type.check_convert_type(capture, Fixnum, :to_int)
+ return nil if index >= match.size || -index >= match.size
+ capture = index
- start = match.begin(capture)
- count = match.end(capture) - match.begin(capture)
- str = self.substring(start, count)
+ str = match[capture]
str.taint if pattern.tainted?
[match, str]
+ private :subpattern
def subpattern_set(pattern, capture, replacement)
unless match = pattern.match(self)
5 spec/tags/19/ruby/core/string/slice_tags.txt
@@ -1,9 +1,4 @@
-fails:String#slice with Regexp, group returns the capture for the given name
fails:String#slice with Regexp, group returns the last capture for duplicate names
fails:String#slice with Regexp, group returns the innermost capture for nested duplicate names
-fails:String#slice with Regexp, group always taints resulting strings when self or regexp is tainted
-fails:String#slice with Regexp, group returns nil if there is no match
fails:String#slice with Regexp, group raises an IndexError if there is no capture for the given name
fails:String#slice with Regexp, group raises an IndexError when given the empty String as a group name
-fails:String#slice with Regexp, group returns subclass instances
-fails:String#slice with Regexp, group sets $~ to MatchData when there is a match and nil when there's none
Something went wrong with that request. Please try again.