开发者

Hash table in Rails

开发者 https://www.devze.com 2022-12-21 23:13 出处:网络
I have the following hash table: COUNTRIES = { \'France\' => \'FR\', \'German\' => \'GE\', \'United Kingdom\' => \'UK\'

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.


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


Try this

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


"Europe".to_country!
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号