Class: String
- Defined in:
- lib/lite/ruby/string.rb,
lib/lite/ruby/safe/string.rb
Constant Summary collapse
- TRANSLITERATIONS =
{ 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'Ae', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'Th', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y', 'Ā' => 'A', 'ā' => 'a', 'Ă' => 'A', 'ă' => 'a', 'Ą' => 'A', 'ą' => 'a', 'Ć' => 'C', 'ć' => 'c', 'Ĉ' => 'C', 'ĉ' => 'c', 'Ċ' => 'C', 'ċ' => 'c', 'Č' => 'C', 'č' => 'c', 'Ď' => 'D', 'ď' => 'd', 'Đ' => 'D', 'đ' => 'd', 'Ē' => 'E', 'ē' => 'e', 'Ĕ' => 'E', 'ĕ' => 'e', 'Ė' => 'E', 'ė' => 'e', 'Ę' => 'E', 'ę' => 'e', 'Ě' => 'E', 'ě' => 'e', 'Ĝ' => 'G', 'ĝ' => 'g', 'Ğ' => 'G', 'ğ' => 'g', 'Ġ' => 'G', 'ġ' => 'g', 'Ģ' => 'G', 'ģ' => 'g', 'Ĥ' => 'H', 'ĥ' => 'h', 'Ħ' => 'H', 'ħ' => 'h', 'Ĩ' => 'I', 'ĩ' => 'i', 'Ī' => 'I', 'ī' => 'i', 'Ĭ' => 'I', 'ĭ' => 'i', 'Į' => 'I', 'į' => 'i', 'İ' => 'I', 'ı' => 'i', 'IJ' => 'Ij', 'ij' => 'ij', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ķ' => 'K', 'ķ' => 'k', 'ĸ' => 'k', 'Ĺ' => 'L', 'ĺ' => 'l', 'Ļ' => 'L', 'ļ' => 'l', 'Ľ' => 'L', 'ľ' => 'l', 'Ŀ' => 'L', 'ŀ' => 'l', 'Ł' => 'L', 'ł' => 'l', 'Ń' => 'N', 'ń' => 'n', 'Ņ' => 'N', 'ņ' => 'n', 'Ň' => 'N', 'ň' => 'n', 'ʼn' => 'n', 'Ŋ' => 'Ng', 'ŋ' => 'ng', 'Ō' => 'O', 'ō' => 'o', 'Ŏ' => 'O', 'ŏ' => 'o', 'Ő' => 'O', 'ő' => 'o', 'Œ' => 'OE', 'œ' => 'oe', 'Ŕ' => 'R', 'ŕ' => 'r', 'Ŗ' => 'R', 'ŗ' => 'r', 'Ř' => 'R', 'ř' => 'r', 'Ś' => 'S', 'ś' => 's', 'Ŝ' => 'S', 'ŝ' => 's', 'Ş' => 'S', 'ş' => 's', 'Š' => 'S', 'š' => 's', 'Ţ' => 'T', 'ţ' => 't', 'Ť' => 'T', 'ť' => 't', 'Ŧ' => 'T', 'ŧ' => 't', 'Ũ' => 'U', 'ũ' => 'u', 'Ū' => 'U', 'ū' => 'u', 'Ŭ' => 'U', 'ŭ' => 'u', 'Ů' => 'U', 'ů' => 'u', 'Ű' => 'U', 'ű' => 'u', 'Ų' => 'U', 'ų' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ŷ' => 'Y', 'ŷ' => 'y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'ź' => 'z', 'Ż' => 'Z', 'ż' => 'z', 'Ž' => 'Z', 'ž' => 'z' }.freeze
Instance Method Summary collapse
- #acronym ⇒ Object
- #acronym! ⇒ Object
- #any?(*keys) ⇒ Boolean
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #capitalized? ⇒ Boolean
- #classify ⇒ Object
- #classify! ⇒ Object
- #constantize ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #deconstantize ⇒ Object
- #deconstantize! ⇒ Object
- #dedupe(pattern) ⇒ Object
- #dedupe!(pattern) ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #each_word(&block) ⇒ Object
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #first(limit = 1) ⇒ Object
- #format(*args) ⇒ Object
- #from(position) ⇒ Object
- #headerize ⇒ Object
- #headerize! ⇒ Object
- #humanize(capitalize: true) ⇒ Object
- #humanize!(capitalize: true) ⇒ Object
- #indent(amount, seperator = ' ') ⇒ Object
- #indent!(amount, seperator = ' ') ⇒ Object
- #index_all(pattern) ⇒ Object
- #labelize(capitalize: true) ⇒ Object (also: #labelcase)
- #labelize!(capitalize: true) ⇒ Object (also: #labelcase!)
- #last(limit = 1) ⇒ Object
- #lchomp(match) ⇒ Object
- #lchomp!(match) ⇒ Object
- #methodize ⇒ Object
- #methodize! ⇒ Object
- #mixedcase? ⇒ Boolean
- #modulize ⇒ Object
- #modulize! ⇒ Object
- #non_possessive ⇒ Object
- #non_possessive! ⇒ Object
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(separator: '-') ⇒ Object
- #parameterize!(separator: '-') ⇒ Object
- #pathize ⇒ Object
- #pathize! ⇒ Object
- #pollute(delimiter = '^--^--^') ⇒ Object
- #pollute!(delimiter = '^--^--^') ⇒ Object
- #pop ⇒ Object
- #possessive ⇒ Object
- #possessive! ⇒ Object
- #possessive? ⇒ Boolean
- #push(string) ⇒ Object
-
#quote(type = :double, amount = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#quote!(type = :double, amount = nil) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #rotate(amount = 1) ⇒ Object
- #rotate!(amount = 1) ⇒ Object
-
#safe_encode(target_encoding, replacement = '') ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#safe_encode!(target_encoding, replacement = '') ⇒ Object
rubocop:enable Metrics/MethodLength.
- #sample(separator = ' ') ⇒ Object
- #sample!(separator = ' ') ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #shuffle(separator = '') ⇒ Object
- #shuffle!(separator = '') ⇒ Object
- #sift(keep) ⇒ Object
- #sift!(keep) ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #sort ⇒ Object
- #sort! ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #titleize ⇒ Object (also: #titlecase)
- #titleize! ⇒ Object (also: #titlecase!)
- #to(position) ⇒ Object
-
#to_time(form = :local) ⇒ Object
(also: #to_t)
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity.
- #transliterize ⇒ Object
- #transliterize! ⇒ Object
-
#truncate(truncate_at, options = {}) ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity.
- #truncate_words(word_count, options = {}) ⇒ Object
- #underscore ⇒ Object (also: #snakecase)
- #underscore! ⇒ Object (also: #snakecase!)
- #unpollute(delimiter = '^--^--^') ⇒ Object
- #unpollute!(delimiter = '^--^--^') ⇒ Object
- #unquote ⇒ Object
- #unquote! ⇒ Object
- #unshift(*patterns) ⇒ Object
- #unshift!(*patterns) ⇒ Object
- #upcase? ⇒ Boolean
- #variablize ⇒ Object
- #variablize! ⇒ Object
- #words ⇒ Object
- #words_without_punctuation ⇒ Object
Instance Method Details
#acronym ⇒ Object
38 39 40 |
# File 'lib/lite/ruby/string.rb', line 38 def acronym gsub(/(([a-zA-Z0-9])([a-zA-Z0-9])*)./, '\\2') || self end |
#acronym! ⇒ Object
42 43 44 |
# File 'lib/lite/ruby/string.rb', line 42 def acronym! replace(acronym) end |
#any?(*keys) ⇒ Boolean
46 47 48 |
# File 'lib/lite/ruby/string.rb', line 46 def any?(*keys) keys.any? { |key| include?(key) } end |
#at(position) ⇒ Object
5 6 7 |
# File 'lib/lite/ruby/safe/string.rb', line 5 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
9 10 11 12 13 14 15 |
# File 'lib/lite/ruby/safe/string.rb', line 9 def camelize(first_letter = :upper) case first_letter when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase } when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase } else modulize end end |
#camelize!(first_letter = :upper) ⇒ Object Also known as: camelcase!
50 51 52 |
# File 'lib/lite/ruby/string.rb', line 50 def camelize!(first_letter = :upper) replace(camelize(first_letter)) end |
#capitalized? ⇒ Boolean
54 55 56 |
# File 'lib/lite/ruby/string.rb', line 54 def capitalized? capitalize == self end |
#classify ⇒ Object
17 18 19 20 21 22 |
# File 'lib/lite/ruby/safe/string.rb', line 17 def classify str = dup str.sub!(/.*\./, '') str.camelize! str || self end |
#classify! ⇒ Object
58 59 60 |
# File 'lib/lite/ruby/string.rb', line 58 def classify! replace(classify) end |
#constantize ⇒ Object
24 25 26 |
# File 'lib/lite/ruby/safe/string.rb', line 24 def constantize Object.const_get(camelize) end |
#dasherize ⇒ Object
28 29 30 |
# File 'lib/lite/ruby/safe/string.rb', line 28 def dasherize underscore.tr('_', '-') end |
#dasherize! ⇒ Object
62 63 64 |
# File 'lib/lite/ruby/string.rb', line 62 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
32 33 34 |
# File 'lib/lite/ruby/safe/string.rb', line 32 def deconstantize [0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
66 67 68 |
# File 'lib/lite/ruby/string.rb', line 66 def deconstantize! replace(deconstantize) end |
#dedupe(pattern) ⇒ Object
70 71 72 |
# File 'lib/lite/ruby/string.rb', line 70 def dedupe(pattern) dup.dedupe!(pattern) end |
#dedupe!(pattern) ⇒ Object
74 75 76 77 |
# File 'lib/lite/ruby/string.rb', line 74 def dedupe!(pattern) pattern.each_char { |char| gsub!(/#{Regexp.escape(char)}{2,}/, char) } self end |
#demodulize ⇒ Object
36 37 38 |
# File 'lib/lite/ruby/safe/string.rb', line 36 def demodulize gsub(/^.*::/, '') end |
#demodulize! ⇒ Object
79 80 81 |
# File 'lib/lite/ruby/string.rb', line 79 def demodulize! replace(demodulize) end |
#domain ⇒ Object
83 84 85 86 87 |
# File 'lib/lite/ruby/string.rb', line 83 def domain return self unless self =~ %r{^(?:\w+://)?([^/?]+)(?:/|\?|$)} Regexp.last_match(1) end |
#downcase? ⇒ Boolean
89 90 91 |
# File 'lib/lite/ruby/string.rb', line 89 def downcase? downcase == self end |
#each_word(&block) ⇒ Object
93 94 95 |
# File 'lib/lite/ruby/string.rb', line 93 def each_word(&block) words.each(&block) end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/lite/ruby/string.rb', line 97 def ellipsize(ellipsize_at, = {}) return self if length <= ellipsize_at separator = [:separator] || '...' offset = [:offset] || 4 "#{self[0, offset]}#{separator}#{self[-offset, offset]}" end |
#first(limit = 1) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/lite/ruby/safe/string.rb', line 40 def first(limit = 1) if limit.zero? '' elsif limit >= length self else to(limit - 1) end end |
#format(*args) ⇒ Object
106 107 108 |
# File 'lib/lite/ruby/string.rb', line 106 def format(*args) super(self, *args.flatten) end |
#from(position) ⇒ Object
50 51 52 |
# File 'lib/lite/ruby/safe/string.rb', line 50 def from(position) self[position..-1] end |
#headerize ⇒ Object
110 111 112 |
# File 'lib/lite/ruby/string.rb', line 110 def headerize squish.each_word(&:capitalize!).join(' ') end |
#headerize! ⇒ Object
114 115 116 |
# File 'lib/lite/ruby/string.rb', line 114 def headerize! replace(headerize) end |
#humanize(capitalize: true) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lite/ruby/safe/string.rb', line 54 def humanize(capitalize: true) str = dup str.underscore! str.delete_suffix!('_id') str.tr!('_', ' ') str.squish! str.gsub!(/([a-z\d]*)/i, &:downcase) str.gsub!(/\A\w/) { |s| capitalize ? s.upcase : s } str || self end |
#humanize!(capitalize: true) ⇒ Object
118 119 120 |
# File 'lib/lite/ruby/string.rb', line 118 def humanize!(capitalize: true) replace(humanize(capitalize: capitalize)) end |
#indent(amount, seperator = ' ') ⇒ Object
65 66 67 |
# File 'lib/lite/ruby/safe/string.rb', line 65 def indent(amount, seperator = ' ') dup.indent!(amount, seperator) end |
#indent!(amount, seperator = ' ') ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/lite/ruby/safe/string.rb', line 69 def indent!(amount, seperator = ' ') if amount >= 0 gsub!(/^/, seperator * amount) else gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '') end end |
#index_all(pattern) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/lite/ruby/string.rb', line 122 def index_all(pattern) pattern = pattern.to_s if pattern.is_a?(Numeric) arr_indexes = [] srch_index = rindex(pattern) while srch_index temp_string = self[0..(srch_index - 1)] arr_indexes << srch_index srch_index = srch_index.zero? ? nil : temp_string.rindex(pattern) end arr_indexes.reverse end |
#labelize(capitalize: true) ⇒ Object Also known as: labelcase
136 137 138 |
# File 'lib/lite/ruby/string.rb', line 136 def labelize(capitalize: true) dup.labelize!(capitalize: capitalize) end |
#labelize!(capitalize: true) ⇒ Object Also known as: labelcase!
140 141 142 143 144 145 146 147 |
# File 'lib/lite/ruby/string.rb', line 140 def labelize!(capitalize: true) underscore! tr!('_', ' ') squish! gsub!(/([a-z\d]*)/i, &:downcase) gsub!(/\A\w/) { |str| capitalize ? str.upcase : str } gsub!(/ id\z/, ' ID') || self end |
#last(limit = 1) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/lite/ruby/safe/string.rb', line 77 def last(limit = 1) if limit.zero? '' elsif limit >= length self else from(-limit) end end |
#lchomp(match) ⇒ Object
149 150 151 |
# File 'lib/lite/ruby/string.rb', line 149 def lchomp(match) dup.lchomp!(match) end |
#lchomp!(match) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/lite/ruby/string.rb', line 153 def lchomp!(match) return self unless index(match) self[0...match.size] = '' self end |
#methodize ⇒ Object
160 161 162 |
# File 'lib/lite/ruby/string.rb', line 160 def methodize dup.methodize! end |
#methodize! ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/lite/ruby/string.rb', line 164 def methodize! gsub!(/([A-Z]+)([A-Z])/, '\1_\2') gsub!(/([a-z])([A-Z])/, '\1_\2') gsub!('/', '__') gsub!('::', '__') downcase! || self end |
#mixedcase? ⇒ Boolean
183 184 185 |
# File 'lib/lite/ruby/string.rb', line 183 def mixedcase? !upcase? && !downcase? end |
#modulize ⇒ Object
172 173 174 |
# File 'lib/lite/ruby/string.rb', line 172 def modulize dup.modulize! end |
#modulize! ⇒ Object
176 177 178 179 180 181 |
# File 'lib/lite/ruby/string.rb', line 176 def modulize! gsub!(/__(.?)/) { "::#{$1.upcase}" } gsub!(%r{/(.?)}) { "::#{$1.upcase}" } gsub!(/(?:_+|-+)([a-z])/) { $1.upcase } gsub!(/(\A|\s)([a-z])/) { $1 + $2.upcase } || self end |
#non_possessive ⇒ Object
187 188 189 |
# File 'lib/lite/ruby/string.rb', line 187 def non_possessive dup.non_possessive! end |
#non_possessive! ⇒ Object
191 192 193 194 195 |
# File 'lib/lite/ruby/string.rb', line 191 def non_possessive! return self unless possessive? chomp!("'s") || chomp!("'") || self end |
#ordinal ⇒ Object
197 198 199 |
# File 'lib/lite/ruby/string.rb', line 197 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
201 202 203 |
# File 'lib/lite/ruby/string.rb', line 201 def ordinalize to_i.ordinalize end |
#parameterize(separator: '-') ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/lite/ruby/safe/string.rb', line 87 def parameterize(separator: '-') str = dup str.underscore! str.gsub!(/\s+/, separator) str.downcase! str || self end |
#parameterize!(separator: '-') ⇒ Object
205 206 207 |
# File 'lib/lite/ruby/string.rb', line 205 def parameterize!(separator: '-') replace(parameterize(separator: separator)) end |
#pathize ⇒ Object
209 210 211 |
# File 'lib/lite/ruby/string.rb', line 209 def pathize dup.pathize! end |
#pathize! ⇒ Object
213 214 215 216 217 218 219 220 221 |
# File 'lib/lite/ruby/string.rb', line 213 def pathize! gsub!(/([A-Z]+)([A-Z])/, '\1_\2') gsub!(/([a-z])([A-Z])/, '\1_\2') gsub!('__', '/') gsub!('::', '/') gsub!(/\s+/, '') gsub!(/[?%*:|"<>.]+/, '') downcase! || self end |
#pollute(delimiter = '^--^--^') ⇒ Object
223 224 225 |
# File 'lib/lite/ruby/string.rb', line 223 def pollute(delimiter = '^--^--^') chars.map { |chr| "#{chr}#{delimiter}" }.join end |
#pollute!(delimiter = '^--^--^') ⇒ Object
227 228 229 |
# File 'lib/lite/ruby/string.rb', line 227 def pollute!(delimiter = '^--^--^') replace(pollute(delimiter)) end |
#pop ⇒ Object
231 232 233 |
# File 'lib/lite/ruby/string.rb', line 231 def pop self[-1] end |
#possessive ⇒ Object
235 236 237 238 239 240 |
# File 'lib/lite/ruby/string.rb', line 235 def possessive return self if possessive? possession = end_with?('s') ? "'" : "'s" "#{self}#{possession}" end |
#possessive! ⇒ Object
242 243 244 |
# File 'lib/lite/ruby/string.rb', line 242 def possessive! replace(possessive) end |
#possessive? ⇒ Boolean
246 247 248 |
# File 'lib/lite/ruby/string.rb', line 246 def possessive? %w['s s'].any? { |pos| end_with?(pos) } end |
#push(string) ⇒ Object
250 251 252 |
# File 'lib/lite/ruby/string.rb', line 250 def push(string) replace(concat(string)) end |
#quote(type = :double, amount = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/lite/ruby/string.rb', line 255 def quote(type = :double, amount = nil) if type.is_a?(Integer) tmp = amount amount = type type = tmp || :mixed else amount ||= 1 end case type.to_s when "'", 'single', 's', '1' f = "'" * amount b = f when '"', 'double', 'd', '2' f = '"' * amount b = f when '`', 'back', 'backtick', 'b', '-1' f = '`' * amount b = f when "`'", 'bracket', 'sb' f = '`' * amount b = "'" * amount when "'\"", 'mixed', 'm', 'Integer' c = (amount.to_f / 2).to_i f = '"' * c b = f if amount.odd? f = "'#{f}" b += "'" end else raise ArgumentError, "Invalid quote type: #{type.inspect}" end "#{f}#{self}#{b}" end |
#quote!(type = :double, amount = nil) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
294 295 296 |
# File 'lib/lite/ruby/string.rb', line 294 def quote!(type = :double, amount = nil) replace(quote(type, amount)) end |
#remove(*patterns) ⇒ Object
95 96 97 |
# File 'lib/lite/ruby/safe/string.rb', line 95 def remove(*patterns) dup.remove!(*patterns) end |
#remove!(*patterns) ⇒ Object
99 100 101 102 103 |
# File 'lib/lite/ruby/safe/string.rb', line 99 def remove!(*patterns) patterns.each_with_object(self) do |pat, str| pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '') end end |
#remove_tags ⇒ Object
298 299 300 |
# File 'lib/lite/ruby/string.rb', line 298 def dup. end |
#remove_tags! ⇒ Object
302 303 304 |
# File 'lib/lite/ruby/string.rb', line 302 def gsub!(%r{</?[^>]*>}, '') || self end |
#rotate(amount = 1) ⇒ Object
306 307 308 |
# File 'lib/lite/ruby/string.rb', line 306 def rotate(amount = 1) dup.rotate!(amount) end |
#rotate!(amount = 1) ⇒ Object
310 311 312 313 |
# File 'lib/lite/ruby/string.rb', line 310 def rotate!(amount = 1) amount += size if amount.negative? slice!(amount, size - amount) + slice!(0, amount) end |
#safe_encode(target_encoding, replacement = '') ⇒ Object
rubocop:disable Metrics/MethodLength
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/lite/ruby/string.rb', line 316 def safe_encode(target_encoding, replacement = '') encode(target_encoding) rescue Encoding::InvalidByteSequenceError force_encoding(target_encoding).scrub!(replacement) rescue Encoding::UndefinedConversionError encode( target_encoding, invalid: :replace, undef: :replace, replace: replacement, UNIVERSAL_NEWLINE_DECORATOR: true ) end |
#safe_encode!(target_encoding, replacement = '') ⇒ Object
rubocop:enable Metrics/MethodLength
331 332 333 |
# File 'lib/lite/ruby/string.rb', line 331 def safe_encode!(target_encoding, replacement = '') replace(safe_encode(target_encoding, replacement)) end |
#sample(separator = ' ') ⇒ Object
335 336 337 |
# File 'lib/lite/ruby/string.rb', line 335 def sample(separator = ' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
339 340 341 |
# File 'lib/lite/ruby/string.rb', line 339 def sample!(separator = ' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
343 344 345 |
# File 'lib/lite/ruby/string.rb', line 343 def shift(*patterns) dup.shift!(*patterns) end |
#shift!(*patterns) ⇒ Object
347 348 349 350 351 |
# File 'lib/lite/ruby/string.rb', line 347 def shift!(*patterns) return self[0] if patterns.empty? patterns.each_with_object(self) { |pat, str| str.sub!(pat, '') } end |
#shuffle(separator = '') ⇒ Object
353 354 355 |
# File 'lib/lite/ruby/string.rb', line 353 def shuffle(separator = '') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
357 358 359 |
# File 'lib/lite/ruby/string.rb', line 357 def shuffle!(separator = '') replace(shuffle(separator)) end |
#sift(keep) ⇒ Object
361 362 363 364 365 366 367 368 369 370 |
# File 'lib/lite/ruby/string.rb', line 361 def sift(keep) keep = case keep when String then keep.chars when Array then keep.map(&:to_s) when Range then keep.to_a.map(&:to_s) else raise TypeError, "Invalid parameter: #{keep.inspect}" end chars.keep_if { |chr| keep.include?(chr) }.join end |
#sift!(keep) ⇒ Object
372 373 374 |
# File 'lib/lite/ruby/string.rb', line 372 def sift!(keep) replace(sift(keep)) end |
#slugify ⇒ Object
376 377 378 |
# File 'lib/lite/ruby/string.rb', line 376 def slugify dup.slugify! end |
#slugify! ⇒ Object
380 381 382 383 384 385 386 |
# File 'lib/lite/ruby/string.rb', line 380 def slugify! gsub!(/[^\x00-\x7F]+/, '') gsub!(/[^\w_ \-]+/i, '') gsub!(/[ \-]+/i, '-') gsub!(/^-|-$/i, '') downcase! || self end |
#sort ⇒ Object
388 389 390 |
# File 'lib/lite/ruby/string.rb', line 388 def sort chars.sort.join end |
#sort! ⇒ Object
392 393 394 |
# File 'lib/lite/ruby/string.rb', line 392 def sort! replace(sort) end |
#squish ⇒ Object
105 106 107 |
# File 'lib/lite/ruby/safe/string.rb', line 105 def squish dup.squish! end |
#squish! ⇒ Object
109 110 111 112 |
# File 'lib/lite/ruby/safe/string.rb', line 109 def squish! strip! gsub!(/\s+/, ' ') || self end |
#titleize ⇒ Object Also known as: titlecase
114 115 116 117 118 119 120 |
# File 'lib/lite/ruby/safe/string.rb', line 114 def titleize str = dup str.underscore! str.humanize! str.gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize } str || self end |
#titleize! ⇒ Object Also known as: titlecase!
404 405 406 |
# File 'lib/lite/ruby/string.rb', line 404 def titleize! replace(titleize) end |
#to(position) ⇒ Object
122 123 124 |
# File 'lib/lite/ruby/safe/string.rb', line 122 def to(position) self[0..position] end |
#to_time(form = :local) ⇒ Object Also known as: to_t
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/lite/ruby/safe/string.rb', line 128 def to_time(form = :local) parts = Date.parse(self, false) used_keys = %i[year mon mday hour min sec sec_fraction offset] return if (parts.keys & used_keys).empty? now = Time.now time = Time.new( parts[:year] || now.year, parts[:mon] || now.month, parts[:mday] || now.day, parts[:hour] || 0, parts[:min] || 0, (parts[:sec] || 0) + (parts[:sec_fraction] || 0), parts[:offset] || (form == :utc ? 0 : nil) ) form == :utc ? time.utc : time.to_time end |
#transliterize ⇒ Object
396 397 398 |
# File 'lib/lite/ruby/string.rb', line 396 def transliterize TRANSLITERATIONS.each_with_object(dup) { |(k, v), s| s.gsub!(k, v) } end |
#transliterize! ⇒ Object
400 401 402 |
# File 'lib/lite/ruby/string.rb', line 400 def transliterize! replace(transliterize) end |
#truncate(truncate_at, options = {}) ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lite/ruby/safe/string.rb', line 149 def truncate(truncate_at, = {}) return self unless length > truncate_at omission = [:omission] || '...' seperator = [:separator] size_with_room_for_omission = truncate_at - omission.length stop = if seperator rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission else size_with_room_for_omission end "#{self[0, stop]}#{omission}" end |
#truncate_words(word_count, options = {}) ⇒ Object
408 409 410 411 412 413 414 415 416 |
# File 'lib/lite/ruby/string.rb', line 408 def truncate_words(word_count, = {}) omission = [:omission] || '...' seperator = [:separator] || /\s+/ seperator = Regexp.escape(seperator.to_s) unless seperator.is_a(Regexp) return self unless /\A((?:.+?#{seperator}){#{word_count - 1}}.+?)#{seperator}.*/m.match?(self) "#{::Regexp.last_match(1)}#{omission}" end |
#underscore ⇒ Object Also known as: snakecase
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/lite/ruby/safe/string.rb', line 166 def underscore str = dup str.camelize! str.gsub!(/::/, '/') str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') str.tr!('-', '_') str.downcase! str || self end |
#underscore! ⇒ Object Also known as: snakecase!
422 423 424 |
# File 'lib/lite/ruby/string.rb', line 422 def underscore! replace(underscore) end |
#unpollute(delimiter = '^--^--^') ⇒ Object
426 427 428 |
# File 'lib/lite/ruby/string.rb', line 426 def unpollute(delimiter = '^--^--^') dup.unpollute!(delimiter) end |
#unpollute!(delimiter = '^--^--^') ⇒ Object
430 431 432 |
# File 'lib/lite/ruby/string.rb', line 430 def unpollute!(delimiter = '^--^--^') gsub!(delimiter, '') || self end |
#unquote ⇒ Object
434 435 436 |
# File 'lib/lite/ruby/string.rb', line 434 def unquote dup.unquote! end |
#unquote! ⇒ Object
438 439 440 441 442 443 444 445 446 |
# File 'lib/lite/ruby/string.rb', line 438 def unquote! [0, -1].each do |i| case self[i, 1] when "'", '"', '`' then self[i] = '' end end self end |
#unshift(*patterns) ⇒ Object
448 449 450 451 |
# File 'lib/lite/ruby/string.rb', line 448 def unshift(*patterns) patterns.each_with_object('') { |pat, str| str << pat } .concat(self) end |
#unshift!(*patterns) ⇒ Object
453 454 455 |
# File 'lib/lite/ruby/string.rb', line 453 def unshift!(*patterns) replace(unshift(*patterns)) end |
#upcase? ⇒ Boolean
418 419 420 |
# File 'lib/lite/ruby/string.rb', line 418 def upcase? upcase == self end |
#variablize ⇒ Object
470 471 472 |
# File 'lib/lite/ruby/string.rb', line 470 def variablize "@#{gsub(/\W/, '_')}" end |
#variablize! ⇒ Object
474 475 476 |
# File 'lib/lite/ruby/string.rb', line 474 def variablize! replace(variablize) end |
#words ⇒ Object
457 458 459 |
# File 'lib/lite/ruby/string.rb', line 457 def words split(/\s+/) end |
#words_without_punctuation ⇒ Object
461 462 463 464 465 466 467 468 |
# File 'lib/lite/ruby/string.rb', line 461 def words_without_punctuation str = dup str.gsub!(%r{[.?¿¡…!,::;—"。?!、‘“”„«»〈〉《》,/\[\]]}, ' ') str.gsub!('- ', ' ') str.squeeze!(' ') str.strip! str.words end |