Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/beef/core_ext/string/titleise.rb,
lib/beef/core_ext/string/to_boolean.rb
Constant Summary collapse
- IGNORE =
%w( the is to a an and as at but by for if in of on or via )
- REG_IGNORE =
IGNORE.join('|')
Instance Method Summary collapse
- #titleise ⇒ Object
- #titleise! ⇒ Object
- #to_boolean ⇒ Object (also: #to_b)
Instance Method Details
#titleise ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/beef/core_ext/string/titleise.rb', line 5 def titleise result = "" self.gsub(/[_-]/, ' ').split(/( [:.;?!][ ] | (?:[ ]|^)["“] )/x).each do |substring| substring.gsub!(/ \b( [[:alpha:]] [[:lower:].'’]* )\b /x) do |word| # Ignore words that contain dots - e.g urls (word =~ / [[:alpha:]] [.] [[:alpha:]] /x) ? word : word.capitalize end #gsub! # Downcase the list of words to ignore substring.gsub!(/\b(#{REG_IGNORE})\b/io) { |word| word.downcase } # If the first or last words are in the ignore list then capetlise them regardless. substring.gsub!(/\A([[:punct:]]*)(#{REG_IGNORE})\b/io) { |word| $1 + $2.capitalize } substring.gsub!(/\b(#{REG_IGNORE})([[:punct:]]*)\Z/io) { |word| $1.capitalize + $2 } result += substring end # Special case "'s" result.gsub!(/(['’])S\b/, '\1s') result end |
#titleise! ⇒ Object
28 29 30 |
# File 'lib/beef/core_ext/string/titleise.rb', line 28 def titleise! self.replace(self.titleise) end |
#to_boolean ⇒ Object Also known as: to_b
3 4 5 6 7 8 |
# File 'lib/beef/core_ext/string/to_boolean.rb', line 3 def to_boolean case self.downcase when 'false', '0', 'no', 'nil', 'null' then false else true end end |