Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Ripper.slice

slice(src, pattern, n = 0)
EXPERIMENTAL

Parses src and return a string which was matched to pattern. pattern should be described as Regexp.

Example
require '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"
See also .sexp_raw .tokenize .sexp #column also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1