Using title case with Ruby 1.8.7
Asked Answered
A

2

2

How can I capitalize certain letters in a string to make it so that only designated words are capitalized.

Must Past These Test: "barack obama" == "Barack Obama" & "the catcher in the rye" == "The Catcher in the Rye"

So far I have a method that will capitalize all words:

#Capitalizes the first title of every word.
def capitalize(words)
     words.split(" ").map {|words| words.capitalize}.join(" ")
end

What are the most efficient next steps I could take to arrive at a solution? Thanks!

Antietam answered 29/10, 2012 at 7:13 Comment(2)
what do you mean by "that only designated words are capitalized", what are "designated" words ?Psoriasis
I apologize, I should've been more clear with the term "title case". Title case capitalizes words that are important to the title or name. As the case with english grammar. Therefore what I meant by saying designated words were the words that MUST be capitalized to make the title functional.Antietam
P
2

You could create a list of the word you don't want to capitalize and do

excluded_words = %w(the and in) #etc

def capitalize_all(sentence, excluded_words)
  sentence.gsub(/\w+/) do |word|
    excluded_words.include?(word) ? word : word.capitalize
  end
end

By the way, if you were using Rails and did not need to exclude specific words you could use titleize.

"the catcher in the rye".titleize
#=> "The Catcher In The Rye"
Physic answered 29/10, 2012 at 7:33 Comment(5)
Seems like a great solution @oldergod, I'll definitely use this after I figure out all the short hand notions you used (i.e. the conditional statement with "?"). I'm very new to programming and I'd like to further explore if there is an easier method that will handle this w/o regexps (haven't gotten there yet). Cheers!Antietam
a ? b : c is the same as if a then b else c end in ruby. It is called a ternary operator.Physic
Hey @oldergod, referencing your code, what if I wanted to capitalize the word "the" if it appeared first. Currently the exclusion method doesn't accomplish this.Antietam
@bigthyme you can add .capitalize after sentence in the first line of the function. That will capitalize the first word of the sentence.Physic
Thanks so much for all the help! Really appreciate it. The books just aren't as interactive as an older God would be.Antietam
L
0

Here is another solution. It's not so pretty, but it deals with acronyms that you want to remain all caps and contractions that you Don'T want to be mangled like my previous use of the contraction don't. Beyond that, it ensures that your first and last word is capitalized.

class String
  def titlecase
    lowerCaseWords = ["a", "aboard", "about", "above", "across", "after", "against", "along", "amid", "among", "an", "and", "around", "as", "at", "before", "behind", "below", "beneath", "beside", "besides", "between", "beyond", "but", "by", "concerning", "considering", "d", "despite", "down", "during", "em", "except", "excepting", "excluding", "following", "for", "from", "in", "inside", "into", "it", "ll", "m", "minus", "near", "nor", "of", "off", "on", "onto", "opposite", "or", "outside", "over", "past", "per", "plus", "re", "regarding", "round", "s", "save", "since", "t", "than", "the", "through", "to", "toward", "towards", "under", "underneath", "unlike", "until", "up", "upon", "ve", "versus", "via", "with", "within", "without", "yet"]
    titleWords = self.gsub( /\w+/ )
    titleWords.each_with_index do | titleWord, i |
      if i != 0 && i != titleWords.count - 1 && lowerCaseWords.include?( titleWord.downcase )
        titleWord
      else
        titleWord[ 0 ].upcase + titleWord[ 1, titleWord.length - 1 ]
      end
    end
  end
end

Here are some examples of how to use it

puts 'barack obama'.titlecase # => Barack Obama
puts 'the catcher in the rye'.titlecase # => The Catcher in the Rye
puts 'NASA would like to send a person to mars'.titlecase # => NASA Would Like to Send a Person to Mars
puts 'Wayne Gretzky said, "You miss 100% of the shots you don\'t take"'.titlecase # => Wayne Gretzky Said, "You Miss 100% of the Shots You Don't Take"
Ligure answered 17/12, 2014 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.