LoginSignup
0
2

More than 5 years have passed since last update.

Rails Console から、ヒュッと number_to_human_size で容量とかを確認する

Last updated at Posted at 2016-10-06

はじめに

  • ちょっとサーバに返却されてる値を GBTB とかみたいにお手軽に確認したい。
  • 9.gigabytes などの 9GB900000 みたいな何かにするには楽です。

NumberToHuman とかでググると

  • 「number_to_human_size ruby」と検索すると、結構ActionView のHelper のAPI の記事がよく出てきます。
  • ですが、consoleで以下のようにやると、
> ActionView::Helpers::NumberHelper.number_to_human_size 10
NoMethodError: undefined method `number_to_human_size' for ActionView::Helpers::NumberHelper:Module
from (pry):5:in `<main>'
  • number_to_human_sizeはmoduleのNumberHelperに定義されてるので呼べません。
  • しかしながらソースを追ってみると、結局ActiveSupport のメソッドにdelegate してることが分かります。

しかし

  • helper という ActionView::Base のインスタンスを rails console から取得可能です。
  • コメント欄にて、@suginoy さんご指摘いただきました、ありがとうございます!
rails_console
> sample = 10.gigabytes
> helper.number_to_human_size sample
=> 10 ギガバイト

結論

  • rails console からでも AtionView のインスタンスの取得が可能です!積極的に使いましょう!

- ActiveSupport にそれっぽい関数があるので、それを呼べばOK です。

pry(main)> ActiveSupport::NumberHelper.number_to_human_size 1024 ** 3
=> "1 GB"
0
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2