Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/title_case.rb

Instance Method Summary collapse

Instance Method Details

#title_case_lineObject

title_case_line capitalizes each ‘word’ in a string, except for words which already have an upper case letter somewhere in them, and except for short articles, prepositions, conjugations and abbreviations which are not the first word of the string.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/title_case.rb', line 35

def title_case_line
  art  = "(a)|(an)|(the)"
  prep = "(on)|(in)|(to)|(by)|(for)|(at)|(of)|(as)|(off)|(as)|(out)|(up)"
  part = "(if)"
  conj = "(and)|(but)|(nor)|(or)|(yet)|(so)"
  abbr = "(re)|(w)|(etc)|(v)|(vs)"
  ignore_raw = '(' + [art, prep, part, conj, abbr].join('|') + ')'
  punct_wrap = "[^[:alpha:]]*"
  ignore = Regexp.new('^' + punct_wrap + ignore_raw + punct_wrap + '$')

  title_case_words =  self.split.map.with_index do |word, index|
    (word =~ /[[:upper:][:digit:]]/) || ( index > 0 && ignore.match(word)) \
      ? word \
      : word.title_case_word
  end

  title_case_words.join(' ')
end

#title_case_wordObject

title_case_word upcases the first alpha character in a string.



24
25
26
27
# File 'lib/title_case.rb', line 24

def title_case_word
  # the non-alpha beginning lets us capitalize "'the'" to "'The'",
  self.sub(/^[^[:alpha:]]*[[:alpha:]]/){|first| first.upcase}
end