1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails6 のちょい足しな新機能を試す48(Duration編)

Posted at

はじめに

Rails 6 に追加されそうな新機能を試す第48段。 今回は、 Duration 編です。
Rails 6 では、 Duration の計算で小数点以下が丸められることが無くなりました。

Ruby 2.6.3, Rails 6.0.0.rc1, Rails 5.2.3 で確認しました。Rails 6.0.0.rc1 は gem install rails --prerelease でインストールできます。

$ rails --version
Rails 6.0.0.rc1

簡単なスクリプトを作る

今回は、動作確認用の簡単なスクリプトを書いて確認します。

bin/duration.rb
duration = 0.4.seconds

d = DateTime.parse('2019-01-01')

d += duration
d += duration
d += duration
d += duration
d += duration

p d > DateTime.parse('2019-01-01')
p d == DateTime.parse('2019-01-01')

Rails 5 では

0.4.seconds を足すとき 整数に丸められて 0.seconds として加算されてしまいます。

結果、 0.4.seconds を5回足す前も後も変化がありません。

$ bin/rails runner bin/duration.rb
false
true

Rails 6 では

整数に丸められることが無くなったので、より正確な値が得られるようになります。

$ bin/rails runner bin/duration.rb
true
false

試したソース

試したソースは以下にあります。
https://github.com/suketa/rails6_0_0rc1/tree/try048_duration

参考情報

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?