Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/classes/string.rb
Instance Method Summary collapse
- #smart_capitalize ⇒ Object
- #smart_capitalize! ⇒ Object
-
#titlecase ⇒ Object
Methods to convert strings to titlecase.
Instance Method Details
#smart_capitalize ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/classes/string.rb', line 20 def smart_capitalize # ignore any leading crazy characters and capitalize the first real character if self =~ /^['"\(\[']*([a-z])/ i = index($1) x = self[i,self.length] # word with capitals and periods mid-word are left alone self[i,1] = self[i,1].upcase unless x =~ /[A-Z]/ or x =~ /\.\w+/ end self end |
#smart_capitalize! ⇒ Object
31 32 33 |
# File 'lib/classes/string.rb', line 31 def smart_capitalize! replace(smart_capitalize) end |
#titlecase ⇒ Object
Methods to convert strings to titlecase. Thanks github.com/samsouder/titlecase
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/classes/string.rb', line 4 def titlecase small_words = %w(a an and as at but by en for if in of on or the to v v. via vs vs.) x = split(" ").map do |word| # note: word could contain non-word characters! # downcase all small_words, capitalize the rest small_words.include?(word.gsub(/\W/, "").downcase) ? word.downcase! : word.smart_capitalize! word end # capitalize first and last words x.first.smart_capitalize! x.last.smart_capitalize! # small words after colons are capitalized x.join(" ").gsub(/:\s?(\W*#{small_words.join("|")}\W*)\s/) { ": #{$1.smart_capitalize} " } end |