[Ruby] I was concerned about all_month and read ActiveSupport DateAndTime::Calculations

1 minute read

What is this

Shouldn’t it be better to use all_month while reviewing with seniors? I was interested, so I looked up and looked up. It was defined in DateAndTime::Calculations of ActiveSupport, so read the surrounding methods

Range method

I looked at the internal implementation of all_month. It is an internal implementation of rails/rails, but you can understand it from the readable code.

def all_month
  beginning_of_month..end_of_month
end

ref: [activesupport/lib/active_support/core_ext/date_and_time/calculations.rb](https://github.com/rails/rails/blob/fc72d6815ad6ed447f1aea2dd98579305ea1cca4/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb#L#

Looking in the same file, there were all_xxx methods other than all_month. There are many methods that can be used because the target range changes.

  • all_day
  • all_month
  • all_quarter: Get date and time information every 3 months
  • all_week: You can decide the start date of the week
  • all_year

Notice 1. future?, past?

There is a method that determines whether it is the future or the past! You can use what is rails/rails without redefining it! !! Notice.

Date.yesterday.past?
=> true
Date.yesterday.future?
=> false
Date.tomorrow.future?
=> true
Date.tomorrow.past?
=> false

Notice 2. next_xxx and prev_xxx exist as well as all_xxx

next_xxx and prev_xxx are defined. prev_xxx is aliased to last_xxx.

Date.current
=> Sun, 11 Jul 2020
Date.current.next_week
=> Mon, 13 Jul 2020
Date.current.prev_week
=> Mon, 29 Jun 2020
Date.current.last_week
=> Mon, 29 Jun 2020

References