当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Ripper.slice用法及代码示例


本文简要介绍ruby语言中 Ripper.slice 的用法。

用法

slice(src, pattern, n = 0)
EXPERIMENTAL

解析 src 并返回与 pattern 匹配的字符串。 pattern 应说明为 Regexp

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"

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Ripper.slice。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。