背景
記事を書いた理由
公式サイトを見ればすぐに分かる何の事はない内容ですが、
ブログだったりQiitaのような、
わかりやすさ重視の非一次情報はなかなか見つからなかったので。
状況
メタプログラミングRubyのMonetizeの例の項を実行していた時。
このような表示に。
ruby $ ruby 202010270014.rb
[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation
Traceback (most recent call last):
10: from 202010270014.rb:5:in `<main>'
9: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money.rb:600:in `format'
8: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:232:in `to_s'
7: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:257:in `format_number'
6: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:333:in `format_whole_part'
5: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:238:in `thousands_separator'
4: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:366:in `lookup'
3: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/locale_backend/legacy.rb:15:in `lookup'
2: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/locale_backend/i18n.rb:19:in `lookup'
1: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/i18n-1.6.0/lib/i18n.rb:182:in `translate'
/Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/i18n-1.6.0/lib/i18n.rb:326:in `enforce_available_locales!': :en is not a valid locale (I18n::InvalidLocale)
ソースコード
エラー発生時
error.rb
require "monetize"
bargain_price = Monetize.from_numeric(99, "USD")
p bargain_price.format
エラー解消時
success.rb
require "monetize"
I18n.load_path << Dir[File.expand_path("config/locales") + "/*.yml"]
I18n.default_locale = :en # (note that `en` is already the default!)
bargain_price = Monetize.from_numeric(99, "USD")
p bargain_price.format
公式リポジトリのREADMEより2,3行目を拝借。
上記のソースの書き換え+config/locales/en.ymlにymlファイルを設置。
その中身は公式リポジトリのREADMEそのままで以下。
en.yml
en:
test: "This is a test"
実行時表示
ruby $ ruby 202010270014.rb
[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation
"$99.00"
WARNINGなんかは出ていますが、一応動作しました。