how to use ruby/rails to convert all caps to proper first letter cap, rest lower case?
I have a data file of names and address...all of them are upper case.
How can I convert it to properly make the first letter upper case, and the rest lower case (except for states, for example)?
I could probably isolate the states to not go through the parse, but for the rest, how could I do thi开发者_JAVA百科s for a ruby on rails application?
If you can separate the states out, like you say, it's easy:
my_address_string.titlecase
It'll capitalize the first letter of every word (including some I'd rather it didn't, like "a" or "the", but hey...) and uncapitalize the rest. Sounds just like what you want.
Xavier's answer is working for me in Rails 2.3.5 environment. Or you can try this
my_address_string.downcase.gsub(/(\A|\s)\w/){ |letter| letter.upcase }
I made a gem to take care of this issue called address_titlecase. It should work for US and Canadian addresses.
精彩评论