Last active
March 22, 2020 20:52
-
-
Save Kameshwaran/2367bc82e6eae6c5cd58 to your computer and use it in GitHub Desktop.
Titleize in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def titleize | |
u_split = split("_") | |
s_split = u_split.map { |s| s.split(" ") }.flatten | |
if s.split.empty? | |
capitalize | |
else | |
s_split.map(&:capitalize).join(" ") | |
end | |
end | |
end | |
# "kamesh waran" => "Kamesh Waran" | |
# "kamesh_waran" => "Kamesh Waran" | |
# "kamesh_waran s" => "Kamesh Waran S" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment