class method
Ripper.slice
(::String src, ::String pattern, ?::Integer n) -> ::String?
slice(src, pattern, n = 0)
ext/ripper/lib/ripper/lexer.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ext/ripper/lib/ripper/lexer.rb, line 277
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"