Skip to content
class method

Ripper.slice

(::String src, ::String pattern, ?::Integer n) -> ::String?
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 #compile_error also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0