[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Convert country name to country code

Petr Janda

3/8/2007 3:39:00 AM

Ive got the TZInfo library installed, and I am able to convert a country
code into country name, but I want to do it the other way around. Is it
possible?

Thanks,
Petr

--
Posted via http://www.ruby-....

2 Answers

Robert Retzbach

3/8/2007 8:38:00 AM

0

On 8 Mrz., 04:39, Petr Janda <p...@punchyouremployer.com> wrote:
> Ive got the TZInfo library installed, and I am able to convert a country
> code into country name, but I want to do it the other way around. Is it
> possible?
>
> Thanks,
> Petr
>
> --
> Posted viahttp://www.ruby-....

I just took a small glimpse on the API doc and think it's not possible
with tzlib, cause they have a countries hash in the Form: {code =>
name}

add the following method to country.rb:

def self.get_code(identifier)
load_index
Indexes::Countries.countries.keys.find{|code| a[code] ==
identifier}
end

Then you should be able to do Country.get_code("Germany").
Good luck :)

Robert Retzbach

3/8/2007 8:40:00 AM

0

On 8 Mrz., 09:38, "rretzbach" <rretzb...@googlemail.com> wrote:
> Indexes::Countries.countries.keys.find{|code| a[code] ==
> identifier}

Sry, too fast too furious!
a should be the same hash:

Indexes::Countries.countries.keys.find{|code|
Indexes::Countries.countries[code] == identifier}

Feel free to improve it :)