Skip to content

Instantly share code, notes, and snippets.

@abrongersma
Created May 20, 2013 22:24
Show Gist options
  • Select an option

  • Save abrongersma/5616082 to your computer and use it in GitHub Desktop.

Select an option

Save abrongersma/5616082 to your computer and use it in GitHub Desktop.
class String
def title_case
stringarray = self.split
returnarray = []
articles = ["and", "of", "the", "a"]
stringarray.each do |a|
if stringarray.rindex(a) == 0
a.capitalize!
returnarray << a + " "
elsif articles.include?(a)
if stringarray.length - 1 == stringarray.rindex(a)
returnarray << a
else
returnarray << a + " "
end
else
a.capitalize!
if stringarray.length - 1 == stringarray.rindex(a)
returnarray << a
else
returnarray << a + " "
end
end
end
returnarray.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment