class String
Specialized titleize
and add_typography
methods.
Constants
- INITIALISMS
- INITIALS_REGEX
- NOT_CAPITALIZED
For AP Style Titles
- WHITESPACES_REGEX
Public Instance Methods
add_html_typography()
click to toggle source
# File lib/law_string.rb, line 97 def add_html_typography gsub(%r{\b(\d+)/(\d+)\b}, '<sup>\1</sup>⁄<sub>\2</sub>') end
add_typography()
click to toggle source
Return a new string enhanced with UTF-8 typographic characters:
Single quotes: ’ Section sign: § Double quotes: “”
# File lib/law_string.rb, line 91 def add_typography tr("'", '’') .gsub(/\bSec\./, '§') .gsub(/"([^"]+)"/, '“\1”') end
capitalize_first_letter()
click to toggle source
# File lib/law_string.rb, line 109 def capitalize_first_letter return '' if self == '' new_string = clone(freeze: false) new_string.capitalize_first_letter! new_string end
capitalize_first_letter!()
click to toggle source
# File lib/law_string.rb, line 117 def capitalize_first_letter! return self if self == '' self[0] = self[0].upcase end
in?(an_array)
click to toggle source
# File lib/law_string.rb, line 15 def in?(an_array) an_array.include?(self) end
initialism?()
click to toggle source
# File lib/law_string.rb, line 23 def initialism? INITIALISMS.include?(self) end
initials?()
click to toggle source
# File lib/law_string.rb, line 19 def initials? INITIALS_REGEX.match?(self) end
md5_sum()
click to toggle source
# File lib/law_string.rb, line 31 def md5_sum Digest::MD5.hexdigest(self) end
starts_with?(str)
click to toggle source
# File lib/law_string.rb, line 27 def starts_with?(str) start_with?(str) end
tail()
click to toggle source
# File lib/law_string.rb, line 74 def tail self[1..-1] end
titleize()
click to toggle source
A better titleize that creates a usable title according to English grammar rules. It's coded to reduce object allocation.
# File lib/law_string.rb, line 39 def titleize new_string = clone(freeze: false) new_string.tr!('_', ' ') final_string = new_string.split(WHITESPACES_REGEX) .map { |w| titleize_word(w) } .join(' ') final_string.capitalize_first_letter! final_string end
titleize!()
click to toggle source
Replace my value with the titleized version.
# File lib/law_string.rb, line 81 def titleize! replace titleize end
titleize_word(word)
click to toggle source
# File lib/law_string.rb, line 50 def titleize_word(word) if word.start_with?('"', '(', '[', '{') extra = word[0] word = word.tail else extra = '' end word.downcase! if NOT_CAPITALIZED.include?(word) # Do nothing elsif word.initials? || word.initialism? word.upcase! else word.capitalize! end if extra == '' word else extra + word end end
utf8_safe()
click to toggle source
Take text with potential encoding problems and aggressively make it safe for UTF-8 import.
# File lib/law_string.rb, line 105 def utf8_safe encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') end