Skip to the content.

monday_of_this_week1.rb(date)

This Ruby snippet utilizes the ‘date’ library to work with dates. It begins by requiring the library. Then, it obtains the current date and calculates the date for the beginning of the current week (Monday) by subtracting the current day’s index from the current date and adding 1. The result is stored in the variable this_monday.

Execution:

require 'date'
#=> true

today = Date.today
#=> #<Date: 2024-12-08 ((2460653j,0s,0n),+0s,2299161j)>
this_monday = today - today.wday + 1
#=> #<Date: 2024-12-09 ((2460654j,0s,0n),+0s,2299161j)>

Executed with Ruby 3.3.6