Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

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-10-10 ((2460594j,0s,0n),+0s,2299161j)>
this_monday = today - today.wday + 1
#=> #<Date: 2024-10-07 ((2460591j,0s,0n),+0s,2299161j)>

Executed with Ruby 3.3.5