Technology Agnostic Rubyist : Open Source Lights The Way

Tuesday, March 31, 2009

Ruby on Rails Inflector for Nationality

I've given myself until midnight to complete the task I wrote about on November 25th.

The idea is for a Ruby Gem that provides a nationalize method for the String Class.

Any country code would be nationalized by simply typing something akin to "France".nationalize => "French" likewise "US".nationalize => "American"

As soon as I found a list country codes... I also realized I wouldn't have enough time to write the code and Gemify it. So I started hacking away at an Environment.rb file.

That got me something like this:
class String
# list of countries derived from http://www.internetworldstats.com/list2.htm#af
def nationalize
{
:Afghanistan => "Afghani",
:Africa => "African",
:Albania => "Albanian",
:Algeria => "Algerian",
:Andorra => "Andorra",
:Angola => "Angolan",
:Anguilla => "Anguilla",
:Antarctica => "Antarctic",
...
}[self.gsub(/_id$/, "").to_sym] || super
end


I decided this was getting a little cumbersome. Especially when I realized that any country with a space (char(32)) or other odd character in the name would need to be typecast as a :symbol in order for my code to work.

Options:
1) Devise out a work around, or
2) Plug away at the entire list of 230+ ISO country names and code for the exceptions

Of course one would prefer a more elegant and flexible solution. Have only two choices seemed awkward. So I did a little bit more research and found an XML list of countries.

This enabled another option:
3) Modify an XML country list to include nationality adjectives.

Now that I've modified the XML country list the data can be parsed, utlized, and published however one sees fit, in any computer program.

This is a flexible solution and completely language agnostic. I like it that way.

While I was thinking about this, I registered the domain name http://originfy.us. I will upload the XML file that I created to that domain.

No comments:

Tweets

Contributors