Class: String
Instance Method Summary collapse
- #any?(*keys) ⇒ Boolean
- #at(position) ⇒ Object
- #camelize(first_letter = :upper) ⇒ Object (also: #camelcase)
- #camelize!(first_letter = :upper) ⇒ Object (also: #camelcase!)
- #classify ⇒ Object
- #classify! ⇒ Object
- #constantize ⇒ Object
- #dasherize ⇒ Object
- #dasherize! ⇒ Object
- #deconstantize ⇒ Object
- #deconstantize! ⇒ Object
- #demodulize ⇒ Object
- #demodulize! ⇒ Object
- #domain ⇒ Object
- #downcase? ⇒ Boolean
- #ellipsize(ellipsize_at, options = {}) ⇒ Object
- #exclude?(string) ⇒ Boolean
- #first(limit = 1) ⇒ Object
- #from(position) ⇒ Object
- #humanize(options = {}) ⇒ Object
- #humanize!(options = {}) ⇒ Object
- #indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #last(limit = 1) ⇒ Object
- #mixedcase? ⇒ Boolean
- #ordinal ⇒ Object
- #ordinalize ⇒ Object
- #parameterize(sep = "-") ⇒ Object
- #parameterize!(sep = "-") ⇒ Object
- #pollute(delimiter = "^--^--^") ⇒ Object
- #remove(*patterns) ⇒ Object
- #remove!(*patterns) ⇒ Object
- #remove_tags ⇒ Object
- #remove_tags! ⇒ Object
- #sample(separator = ' ') ⇒ Object
- #sample!(separator = ' ') ⇒ Object
- #shift(*patterns) ⇒ Object
- #shift!(*patterns) ⇒ Object
- #shuffle(separator = '') ⇒ Object
- #shuffle!(separator = '') ⇒ Object
- #slugify ⇒ Object
- #slugify! ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #titleize ⇒ Object (also: #titlecase)
- #titleize! ⇒ Object (also: #titlecase!)
- #to(position) ⇒ Object
- #truncate(truncate_at, options = {}) ⇒ Object
- #truncate_words(words_count, options = {}) ⇒ Object
- #underscore ⇒ Object
- #underscore! ⇒ Object
- #unpollute(delimiter = "^--^--^") ⇒ Object
- #upcase? ⇒ Boolean
Instance Method Details
#any?(*keys) ⇒ Boolean
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/active_object/string.rb', line 3 def any?(*keys) included = false keys.flatten.each do |key| (included = true) if include?(key) break if included end return(included) end |
#at(position) ⇒ Object
15 16 17 |
# File 'lib/active_object/string.rb', line 15 def at(position) self[position] end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
21 22 23 24 25 26 27 |
# File 'lib/active_object/string.rb', line 21 def camelize(first_letter=:upper) if first_letter != :lower to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else to_s.first.chr.downcase + camelize(self)[1..-1] end end |
#camelize!(first_letter = :upper) ⇒ Object Also known as: camelcase!
33 34 35 |
# File 'lib/active_object/string.rb', line 33 def camelize!(first_letter=:upper) replace(camelize(first_letter)) end |
#classify ⇒ Object
41 42 43 |
# File 'lib/active_object/string.rb', line 41 def classify to_s.sub(/.*\./, "").camelize end |
#classify! ⇒ Object
47 48 49 |
# File 'lib/active_object/string.rb', line 47 def classify! replace(classify) end |
#constantize ⇒ Object
53 54 55 |
# File 'lib/active_object/string.rb', line 53 def constantize Object.const_get(self) end |
#dasherize ⇒ Object
59 60 61 |
# File 'lib/active_object/string.rb', line 59 def dasherize gsub(/_/, "-") end |
#dasherize! ⇒ Object
65 66 67 |
# File 'lib/active_object/string.rb', line 65 def dasherize! replace(dasherize) end |
#deconstantize ⇒ Object
71 72 73 |
# File 'lib/active_object/string.rb', line 71 def deconstantize to_s[0, rindex('::') || 0] end |
#deconstantize! ⇒ Object
77 78 79 |
# File 'lib/active_object/string.rb', line 77 def deconstantize! replace(deconstantize) end |
#demodulize ⇒ Object
83 84 85 |
# File 'lib/active_object/string.rb', line 83 def demodulize to_s.gsub(/^.*::/, "") end |
#demodulize! ⇒ Object
89 90 91 |
# File 'lib/active_object/string.rb', line 89 def demodulize! replace(demodulize) end |
#domain ⇒ Object
94 95 96 97 |
# File 'lib/active_object/string.rb', line 94 def domain string = dup string =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : string end |
#downcase? ⇒ Boolean
99 100 101 |
# File 'lib/active_object/string.rb', line 99 def downcase? downcase == self end |
#ellipsize(ellipsize_at, options = {}) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/active_object/string.rb', line 103 def ellipsize(ellipsize_at, ={}) return(self)if size <= ellipsize_at separator = .fetch(:separator, "...") offset = .fetch(:offset, 4) "#{self[0,offset]}#{separator}#{self[-offset,offset]}" end |
#exclude?(string) ⇒ Boolean
112 113 114 |
# File 'lib/active_object/string.rb', line 112 def exclude?(string) !include?(string) end |
#first(limit = 1) ⇒ Object
118 119 120 121 |
# File 'lib/active_object/string.rb', line 118 def first(limit=1) return("") if limit.zero? limit >= size ? self.dup : to(limit - 1) end |
#from(position) ⇒ Object
125 126 127 |
# File 'lib/active_object/string.rb', line 125 def from(position) self[position..-1] end |
#humanize(options = {}) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/active_object/string.rb', line 131 def humanize( = {}) underscore. gsub(/_id\z/, ""). tr("_", " "). gsub(/([a-z\d]*)/i) { |s| s.downcase }. gsub(/\A\w/) { |s| .fetch(:capitalize, true) ? s.upcase : s } end |
#humanize!(options = {}) ⇒ Object
141 142 143 |
# File 'lib/active_object/string.rb', line 141 def humanize!(={}) replace(humanize()) end |
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
147 148 149 150 151 |
# File 'lib/active_object/string.rb', line 147 def indent(amount, indent_string=nil, indent_empty_lines=false) indent_string = indent_string || self[/^[ \t]/] || " " substitutes = indent_empty_lines ? /^/ : /^(?!$)/ gsub(substitutes, indent_string * amount) end |
#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
155 156 157 |
# File 'lib/active_object/string.rb', line 155 def indent!(amount, indent_string=nil, indent_empty_lines=false) replace(indent(amount, indent_string, indent_empty_lines)) end |
#last(limit = 1) ⇒ Object
161 162 163 164 |
# File 'lib/active_object/string.rb', line 161 def last(limit=1) return("") if limit.zero? limit >= size ? self.dup : from(-limit) end |
#mixedcase? ⇒ Boolean
167 168 169 |
# File 'lib/active_object/string.rb', line 167 def mixedcase? !upcase? && !downcase? end |
#ordinal ⇒ Object
172 173 174 |
# File 'lib/active_object/string.rb', line 172 def ordinal to_i.ordinal end |
#ordinalize ⇒ Object
178 179 180 |
# File 'lib/active_object/string.rb', line 178 def ordinalize to_i.ordinalize end |
#parameterize(sep = "-") ⇒ Object
184 185 186 187 188 189 |
# File 'lib/active_object/string.rb', line 184 def parameterize(sep="-") underscore. gsub(/\s+/, "_"). gsub("_", sep). downcase end |
#parameterize!(sep = "-") ⇒ Object
193 194 195 |
# File 'lib/active_object/string.rb', line 193 def parameterize!(sep="-") replace(parameterize(sep)) end |
#pollute(delimiter = "^--^--^") ⇒ Object
198 199 200 |
# File 'lib/active_object/string.rb', line 198 def pollute(delimiter="^--^--^") split('').map { |c| "#{c}#{delimiter}" }.join end |
#remove(*patterns) ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/active_object/string.rb', line 203 def remove(*patterns) string = dup patterns.flatten.each do |pattern| string.gsub!(pattern, "") end string end |
#remove!(*patterns) ⇒ Object
213 214 215 |
# File 'lib/active_object/string.rb', line 213 def remove!(*patterns) replace(remove(*patterns)) end |
#remove_tags ⇒ Object
218 219 220 |
# File 'lib/active_object/string.rb', line 218 def gsub(/<\/?[^>]*>/, "") end |
#remove_tags! ⇒ Object
222 223 224 |
# File 'lib/active_object/string.rb', line 222 def replace() end |
#sample(separator = ' ') ⇒ Object
226 227 228 |
# File 'lib/active_object/string.rb', line 226 def sample(separator=' ') split(separator).sample end |
#sample!(separator = ' ') ⇒ Object
230 231 232 |
# File 'lib/active_object/string.rb', line 230 def sample!(separator=' ') replace(sample(separator)) end |
#shift(*patterns) ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/active_object/string.rb', line 234 def shift(*patterns) string = dup patterns.flatten.each do |pattern| string.sub!(pattern, "") end string end |
#shift!(*patterns) ⇒ Object
242 243 244 |
# File 'lib/active_object/string.rb', line 242 def shift!(*patterns) replace(shift(*patterns)) end |
#shuffle(separator = '') ⇒ Object
246 247 248 |
# File 'lib/active_object/string.rb', line 246 def shuffle(separator='') split(separator).shuffle.join end |
#shuffle!(separator = '') ⇒ Object
250 251 252 |
# File 'lib/active_object/string.rb', line 250 def shuffle!(separator='') replace(shuffle(separator)) end |
#slugify ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/active_object/string.rb', line 254 def slugify gsub(/[^\x00-\x7F]+/, ''). gsub(/[^\w_ \-]+/i, ''). gsub(/[ \-]+/i, '-'). gsub(/^\-|\-$/i, ''). downcase end |
#slugify! ⇒ Object
262 263 264 |
# File 'lib/active_object/string.rb', line 262 def slugify! replace(slugify) end |
#squish ⇒ Object
267 268 269 |
# File 'lib/active_object/string.rb', line 267 def squish strip.gsub(/\s+/, ' ') end |
#squish! ⇒ Object
273 274 275 |
# File 'lib/active_object/string.rb', line 273 def squish! replace(squish) end |
#titleize ⇒ Object Also known as: titlecase
279 280 281 282 283 |
# File 'lib/active_object/string.rb', line 279 def titleize underscore. humanize. gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize } end |
#titleize! ⇒ Object Also known as: titlecase!
289 290 291 |
# File 'lib/active_object/string.rb', line 289 def titleize! replace(titleize) end |
#to(position) ⇒ Object
297 298 299 |
# File 'lib/active_object/string.rb', line 297 def to(position) self[0..position] end |
#truncate(truncate_at, options = {}) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/active_object/string.rb', line 303 def truncate(truncate_at, ={}) return(dup) unless size > truncate_at omission = .fetch(:omission, "...") size_with_room_for_omission = truncate_at - omission.size stop = if .fetch(:separator, false) rindex(.fetch(:separator, ''), size_with_room_for_omission) || size_with_room_for_omission else size_with_room_for_omission end "#{self[0, stop]}#{omission}" end |
#truncate_words(words_count, options = {}) ⇒ Object
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/active_object/string.rb', line 320 def truncate_words(words_count, ={}) sep = [:separator] || /\s+/ sep = Regexp.escape(sep.to_s) unless Regexp === sep if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m $1 + ([:omission] || '...') else dup end end |
#underscore ⇒ Object
333 334 335 336 337 338 339 |
# File 'lib/active_object/string.rb', line 333 def underscore gsub(/::/, '/'). gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
#underscore! ⇒ Object
343 344 345 |
# File 'lib/active_object/string.rb', line 343 def underscore! replace(underscore) end |
#unpollute(delimiter = "^--^--^") ⇒ Object
348 349 350 |
# File 'lib/active_object/string.rb', line 348 def unpollute(delimiter="^--^--^") gsub(delimiter, "") end |
#upcase? ⇒ Boolean
352 353 354 |
# File 'lib/active_object/string.rb', line 352 def upcase? upcase == self end |