I want to convert the name "JOHN DOE-SMITH" to "John Doe-Smith" using the code:
name = "JOHN DOE-SMITH"
name_split = name.split(/\s/)
name_split.each do |x|
if x =~ /-/
name1, name2 = x.split(/-/)
x = name1.capitalize + "-" + name2.capitalize
else
x.capitalize!
end
end
puts name_split.join(" ")
The result is the unexpected "John DOE-SMITH"
Why does x.capitalize! have an effect while x = "foo" has none?
Is there a better way to do this?