0
1

More than 1 year has passed since last update.

【Ruby】あと何日かを求める

Last updated at Posted at 2021-10-15

環境

Ruby 3.0.2

前提

12月10日までに100記事目標にしている自分に対して、今の投稿進捗を教えてくれるbotに台詞を追加したい。
(実際のbotの書き方とは少し異なる)
あと何日か、1日何記事ペースで書いたらいいのかをrubyで書くときに少し詰まったので記事に残す。
現在27記事投稿済み。
Date.todayは2021年10月15日とする。

完成形

  • マジックナンバーは定数化する
require 'date'

today = Date.today
limit = Date.new(2021, 12, 10)
target_articles_count = 100
articles_count = 27
off_days_count = 18

# 残りの日にちから土日祝の18日分を引く
remaining_days_count = (limit - today).to_i - off_days_count
per_day = ((target_articles_count - articles_count) / remaining_days_count.to_f).round(1)

p "本日までの記事投稿数は#{articles_count}記事になりました!!\n目標まであと#{target_articles_count - articles_count}記事、1日#{per_day}記事ペースで目標達成!\n引き続きがんばれ〜!"

残り何日かを求める

today = Date.today
limit = Date.new(2021, 12, 10)
remaining_days_count = (limit - today).to_i
p remaining_days_count
#=>56

to_iしないと分数の形になる

remaining_days_count = limit - today
p remaining_days_count
#=>56/1
0
1
1

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
1