Right now I have
value = "United states of america"
words_to_ignore = ["the","of"]
new_string = value.split(' ').map {|w| w.capitalize }.join(' ')
What I am trying to do here is except the word of
, I want the rest capitalized. So the output would be United States of America
. Now I am not sure, how exactly to do this.
Try this:
new_string = value.split(' ')
.each{|i| i.capitalize! if ! words_to_ignore.include? i }
.join(' ')
value = "United state of america"
words_to_ignore = Hash[%w[the of].map{|w| [w, w]}]
new_string = value.gsub(/\w+/){|w| words_to_ignore[w] || w.capitalize}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With