Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 this for a ruby on rails application?

like image 531
Satchel Avatar asked Feb 07 '11 01:02

Satchel


People also ask

How do you change uppercase to lowercase in Ruby?

Ruby has a few methods for changing the case of strings. To convert to lowercase, use downcase : "hello James!". downcase #=> "hello james!"

How do I make the first letter uppercase in Ruby?

Ruby | String capitalize() Method capitalize is a String class method in Ruby which is used to return a copy of the given string by converting its first character uppercase and the remaining to lowercase.


1 Answers

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.

like image 188
Xavier Holt Avatar answered Oct 08 '22 00:10

Xavier Holt