KDOC 706: RubyのDate同士の引き算はRationalを返す
この文書のステータス
- 作成
- 2026-10-18 貴島
- レビュー
- 2026-10-18 貴島
概要
RubyでDate同士を引くと、日数が整数ではなく有理数(Rational)で返る。表示すると (1/1) のようになる。整数の日数が欲しいときは to_i する。
If the other is a date object, returns a Rational whose value is the difference between the two dates in days.
require 'date'
d = Date.new(2024, 1, 2) - Date.new(2024, 1, 1)
puts "そのまま: #{d.inspect}、クラス: #{d.class}、to_i: #{d.to_i}"
そのまま: (1/1)、クラス: Rational、to_i: 1
関連
なし。