Rubyでは、Dateのインスタンスをinspectすると、以下のような馴染みのない数値が出力されます。

   1  Date.today.inspect
   2  => "#<Date: 4909185/2,0,2299161>"

これはDateクラスが日付の内部表現としてRationalを利用しているためですが、inspectしたときにこの表現が出てきても、日付がわからないので困りますね。 そこで、以下のようにDate#inspectを定義してあげると、 人間に優しい表現になります。

   1  class Date
   2    def inspect
   3      strftime '#<Date: %x>'
   4    end
   5  end
   6  
   7  >> Date.today
   8  => #<Date: 05/06/08>

これならDateが何日なのかわかりやすいですね。

strftime%xはロケールによって出力が変わるので、 お使いのPCの環境によっては、上記のような出力にならない事もあります。

See Also

posted by Png genki on Tue 6 May 2008 at 11:44
Contents
Date#inspectをわかりやすくする
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ