class Schizm::String
Extend the default String
class.
Public Instance Methods
Format as a C-style comment.
# File lib/schizm/string.rb, line 145 def comment s = "/*" each_line do |line| s.concat " " s.concat line s.concat " *" end s.concat "/" end
Erase occurances of +Regexp.union(*args)+.
# File lib/schizm/string.rb, line 39 def erase *args gsub Regexp.union(*args), "" end
Erase occurances of +Regexp.union(*args)+.
# File lib/schizm/string.rb, line 34 def erase! *args gsub! Regexp.union(*args), "" end
Expand tabs, subject to tabstop stop
.
# File lib/schizm/string.rb, line 128 def expand_tab stop gsub /([^\t\n]*)\t/u do $1 + " " * (stop - $1.size % stop) end end
Expand tabs, subject to tabstop stop
.
# File lib/schizm/string.rb, line 121 def expand_tab! stop gsub! /([^\t\n]*)\t/u do $1 + " " * (stop - $1.size % stop) end end
Distance from last newline to length
. If there's no last newline, just length
.
# File lib/schizm/string.rb, line 136 def find_indent if (offset = rindex /\n/u) length - offset - 1 else length end end
Make suitable string for include guard.
# File lib/schizm/string.rb, line 61 def guardify strip.wordify.upcase end
Encode non-ASCII characters and '&', '<', and '>' as HTML entities.
# File lib/schizm/string.rb, line 73 def html gsub /[&<>\p{^ASCII}]/u do |char| '&#' + char.codepoints[0].to_s + ';' end end
Encode non-ASCII characters and '&', '<', and '>' as HTML entities.
# File lib/schizm/string.rb, line 66 def html! gsub! /[&<>\p{^ASCII}]/u do |char| '&#' + char.codepoints[0].to_s + ';' end end
Replace space sequences by a single ASCII space.
# File lib/schizm/string.rb, line 116 def slim gsub /\p{Space}+/u, " " end
Replace space sequences by a single ASCII space.
# File lib/schizm/string.rb, line 111 def slim! gsub! /\p{Space}+/u, " " end
Make suitable string for slug.
# File lib/schizm/string.rb, line 54 def slugify gsub(/^\p{^Word}+/u, "") .gsub(/\p{^Word}+$/u, "") .gsub(/\p{^Word}+/u, '-').downcase end
Remove blanks preceding newlines, remove trailing spaces, remove newlines succeeding empty lines, and remove leading newlines.
# File lib/schizm/string.rb, line 104 def trim copy = String.new self copy.trim! copy end
Remove blanks preceding newlines, remove trailing spaces, remove newlines succeeding empty lines, and remove leading newlines.
# File lib/schizm/string.rb, line 92 def trim! gsub! /\p{Blank}+\n/u, "\n" gsub! /\p{Space}+\z/u, "" gsub! /\n\n+/u, "\n\n" while start_with? "\n" slice! 0 end end
Remove blanks preceding newlines.
# File lib/schizm/string.rb, line 85 def trim_blank gsub /\p{Blank}+\n/u, "\n" end
Remove blanks preceding newlines.
# File lib/schizm/string.rb, line 80 def trim_blank! gsub! /\p{Blank}+\n/u, "\n" end
For sentence-like strings. Truncate to n
characters and replace the final broken word with an ellipsis.
# File lib/schizm/string.rb, line 178 def truncate n if size <= n return String.new self else return String.new(self[0..n]) .gsub(/\p{Space}+/u, " ") .gsub(/\p{Space}+\p{Graph}+\z/u, "...") end end
Remove backslashes which escape characters chars
.
# File lib/schizm/string.rb, line 156 def unescape *chars s = String.new prev_char_bslash = false each_char do |char| if prev_char_bslash prev_char_bslash = false unless chars.include? char s.concat '\\' end else if char == '\\' prev_char_bslash = true next end end s.concat char end s end
Replace sequences of +p{^Word}+ by an underscore.
# File lib/schizm/string.rb, line 49 def wordify gsub /\p{^Word}+/u, '_' end
Replace sequences of +p{^Word}+ by an underscore.
# File lib/schizm/string.rb, line 44 def wordify! gsub! /\p{^Word}+/u, '_' end