I\'m trying to convert an all-uppercase string in Ruby into a lower case one, but with each word\'s first character being upper case. Example:
convert \"MY STRING HE
string = "MY STRING HERE"
string.split(" ").map {|word| word.capitalize}.join(" ")
The way this works:
The .split(" ")
splits it on spaces, so now we have an array that looks like ["my", "string", "here"]
. The map
call iterates over each element of the array, assigning it to temporary variable word
, which we then call capitalize
on. Now we have an array that looks like ["My", "String", "Here"]
, and finally we turn that array back into a string by join
ing each element with a space (" ").