Help us understand the problem. What is going on with this article?

Rubyで残り日数の計算

More than 1 year has passed since last update.

Rubyで残り日数を表示しようとした時、不思議な動きをしたのでメモ代わりにまとめます

まず、思いついたままやってみました

remaining_day.rb
require 'date'
today = Date.new(2018, 2, 13)   
future = Date.new(2018, 2, 23)

remainingDay = future -today

puts remainingDay  # => 10/1

なんか分数の形なるし、

remaining_day.rb
puts remainingDay.class # => Rational

Rationalクラスというのがあるらしいです
ドキュメントにはこんな感じで書いてました、へーって感じです

要約
有理数を扱うクラスです。
「1/3」のような有理数を扱う事ができます

メソッドを探したら、分子のみを返すメソッドがありました

remaining_day.rb
puts remainingDay.numerator # => 10
puts remaininDay.class      # => Integer

無事残り日数が取得できました

なんですけど、こっちでもいけました

remaining_day.rb
puts remainingDay.to_i # => 10
puts remaininDay.class # => Integer

Integerは整数クラスやから、
.to_i使えば、整数に変換できるから、必然的に分子のみになる

難しいこと考えんでも、簡単でした

読んでいただきありがとうございます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away