Adnan
Adnan

Reputation: 26360

Hash table in Rails

I have the following hash table:

  COUNTRIES = {
  'France' => 'FR', 
  'German' => 'GE', 
  'United Kingdom' => 'UK'
  }

I have it in my model and use it in my views so the countries are displayed as a select box. Now I have one view where I want all those values plus one more value "Europe" => 'EU' to be shown. Meaning I would have:

  COUNTRIES = {
  'Europe' => 'EU', 
  'France' => 'FR', 
  'German' => 'GE', 
  'United Kingdom' => 'UK'
  }

Now I can create a new hash table but I dont want to repeat the same values in a new table.

So, how can I re-use the same table, adding one more value just for a particular view?

All ideas are welcome.

Upvotes: 2

Views: 3565

Answers (3)

Aurril
Aurril

Reputation: 2469

customCountries = COUNTRIES.clone
customCountries['Europe'] = 'EU'

Upvotes: 4

Jonas Elfström
Jonas Elfström

Reputation: 31458

Try this

custom = {'Europe' => 'EU'}.merge(COUNTRIES)

Upvotes: 3

Peter Brown
Peter Brown

Reputation: 51717

"Europe".to_country!

Upvotes: 1

Related Questions