【问题标题】:Is it possible to filter list of Ruby money currencies to only active ones?是否可以将 Ruby 货币列表过滤为仅活跃的货币?
【发布时间】:2017-04-06 16:13:55
【问题描述】:

我在 Rails 应用程序中使用money gem 进行货币转换,需要为表单选择标签生成货币列表。

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

有没有办法只获取有效 ISO 货币代码的列表而不维护我自己的列表?

【问题讨论】:

标签: ruby currency money-rails


【解决方案1】:

金钱宝石确实包括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}}
# }

但是请注意,这是一个私有方法,因此它可能会在未来的版本中发生变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2014-03-25
    • 2014-08-28
    相关资源
    最近更新 更多