End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
Ripper.slice
slice(src, pattern, n = 0)
ext/ripper/lib/ripper/lexer.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/ext/ripper/lib/ripper/lexer.rb, line 270
def Ripper.slice(src, pattern, n = 0)
if m = token_match(src, pattern)
then m.string(n)
else nil
end
end
- EXPERIMENTAL
-
Parses
srcand return a string which was matched topattern.patternshould be described asRegexp.Examplerequire 'ripper' p Ripper.slice('def m(a) nil end', 'ident') #=> "m" p Ripper.slice('def m(a) nil end', '[ident lparen rparen]+') #=> "m(a)" p Ripper.slice("<<EOS\nstring\nEOS", 'heredoc_beg nl $(tstring_content*) heredoc_end', 1) #=> "string\n"