Skip to content

Instantly share code, notes, and snippets.

@Kameshwaran
Last active March 22, 2020 20:52
Show Gist options
  • Save Kameshwaran/2367bc82e6eae6c5cd58 to your computer and use it in GitHub Desktop.
Save Kameshwaran/2367bc82e6eae6c5cd58 to your computer and use it in GitHub Desktop.
Titleize in ruby
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