核心扩展字符串过滤器
字符串#挤
返回给定字符串的版本,不带前导或尾随空格,并将内部的所有连续空格组合为单个空格。破坏性版本 squish!
直接在字符串实例上运行。
处理 ASCII 和 Unicode 空白。
%{ Multi-line
string }.squish # => "Multi-line string"
" foo bar
boo".squish # => "foo bar boo"
字符串#删除
返回一个新字符串,其中删除了所有出现的模式。破坏性版本 remove!
直接在给定的字符串上运行。
str = "foo bar test"
str.remove(" test") # => "foo bar"
str.remove(" test", /bar/) # => "foo "
字符串#截断
如果字符串长度超过长度,则返回以给定长度截断的给定字符串的副本。
'Once upon a time in a world far far away'.truncate(27)
# => "Once upon a time in a wo..."
传递一个字符串或正则表达式:separator
在自然休息时截断
'Once upon a time in a world far far away'.truncate(27, separator: ' ')
# => "Once upon a time in a..."
'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
# => "Once upon a time in a..."
字符串#truncate_words
返回在给定数量的单词后截断的字符串。
'Once upon a time in a world far far away'.truncate_words(4)
# => "Once upon a time..."
传递字符串或正则表达式以指定不同的单词分隔符
'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>')
# => "Once<br>upon<br>a<br>time<br>in..."
最后一个字符将替换为:omission
字符串(默认为“…”)
'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
# => "And they found that many... (continued)"
字符串#strip_heredoc
剥离 heredocs 中的缩进。查找最少缩进的非空行并删除前导空格的数量。
if options[:usage]
puts <<-USAGE.strip_heredoc
This command does such and such.
Supported options are:
-h This message
...
USAGE
end
用户会看到
This command does such and such.
Supported options are:
-h This message
...