Reputation: 35
I am trying to make a ruby def that looks like this:
def title_case(title, minor_words)
end
It's supposed to title case the string variable "title", any word in the minor_words variable is to be downcased and any word not it the minor_words variable is to be capitalized.
Example:
title_case("The King is a Good man", "a and the an at")
-> "the King Is a Good Man"
And the code I am attempting to write looks like this:
def title_case(title, minor_words)
title = title.downcase.split
minor_words = minor_words.downcase.split
title.map {|word| if word == (any word from minor_words)
word.downcase
else
word.capitalize
end}.join(" ")
end
If minor_words is empty it returns title with all letters capitalized. Like so:
-> "The King Is A Good Man"
If title is empty, or both are empty it returns an empty string:
-> ""
Hope I made things clear! Thanks!
Upvotes: 0
Views: 88
Reputation: 7627
You could use a ternary statement:
ary1.map { |e| ary2.include?(e.downcase) ? e.downcase : e }
# => ["the", "man", "is", "not", "nice"]
To mutate the array in place use Array#map!
instead of Array#map
.
Upvotes: 2