2017-04-06 67 views
-1

我在Rails应用中使用money gem进行货币换算,并需要为表单选择标记生成货币列表。是否可以将Ruby货币列表过滤为只有活动的货币?

建议的文件依赖拨打Money::Currency.table获取所有货币的列表,但这包括非iso货币(例如比特币)和旧货币(例如三个无效的津巴布韦美元条目)。

有没有办法只获取一个有效的ISO货币代码列表,而无需维护我自己的列表?

+0

https://www.currency-iso.org/dam/downloads/lists/list_one.xml – JLB

+0

当问到“是否有办法”时,您的问题立即变得非常宽泛,除非您显示您尝试的内容并缩小范围的查询。 “有没有办法...?” “是”不会帮助任何人。 “我尝试了这一点,但他们没有工作”缩小了问题范围,使我们无法猜测你真正想问什么。请阅读“[问]”和链接页面,“[mcve]”及其链接页面,以及“[Stack Overflow用户需要多少研究工作?](http://meta.stackoverflow.com/questions/ 261592)“这将有助于解释为什么我们需要更多的细节和更具体的问题。 –

回答

0

钱宝石不包括separate lists,但不幸的是,他们是merged together

def load_currencies 
    currencies = parse_currency_file("currency_iso.json") 
    currencies.merge! parse_currency_file("currency_non_iso.json") 
    currencies.merge! parse_currency_file("currency_backwards_compatible.json") 
end 

您可以手动调用代码获得刚currency_iso.json列表:但是这是

Money::Currency.send(:parse_currency_file, 'currency_iso.json') 
#=> { 
# :aed=>{:priority=>100, :iso_code=>"AED", :name=>"United Arab Emirates Dirham", :symbol=>"د.إ", :alternate_symbols=>["DH", "Dhs"], :subunit=>"Fils", :subunit_to_unit=>100, :symbol_first=>true, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"784", :smallest_denomination=>25}, 
# :afn=>{:priority=>100, :iso_code=>"AFN", :name=>"Afghan Afghani", :symbol=>"؋", :alternate_symbols=>["Af", "Afs"], :subunit=>"Pul", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"971", :smallest_denomination=>100}, 
# :all=>{:priority=>100, :iso_code=>"ALL", :name=>"Albanian Lek", :symbol=>"L", :disambiguate_symbol=>"Lek", :alternate_symbols=>["Lek"], :subunit=>"Qintar", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"008", :smallest_denomination=>100}, 
# ... 
# :zar=>{:priority=>100, :iso_code=>"ZAR", :name=>"South African Rand", :symbol=>"R", :alternate_symbols=>[], :subunit=>"Cent", :subunit_to_unit=>100, :symbol_first=>true, :html_entity=>"R", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"710", :smallest_denomination=>10}, 
# :zmk=>{:priority=>100, :iso_code=>"ZMK", :name=>"Zambian Kwacha", :symbol=>"ZK", :disambiguate_symbol=>"ZMK", :alternate_symbols=>[], :subunit=>"Ngwee", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"894", :smallest_denomination=>5}, 
# :zmw=>{:priority=>100, :iso_code=>"ZMW", :name=>"Zambian Kwacha", :symbol=>"ZK", :disambiguate_symbol=>"ZMW", :alternate_symbols=>[], :subunit=>"Ngwee", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"967", :smallest_denomination=>5}} 
# } 

注一个私人的方法,所以它可能在未来的版本中改变