Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

monday_of_this_week2.rb(date)

This Ruby snippet captures the current date and time, then calculates and assigns the date of the most recent Monday by subtracting the number of seconds corresponding to the days elapsed since the beginning of the week.

Execution:

today = Time.now
#=> 2024-10-10 15:57:31.39865918 +0000
this_monday = today - (today.wday - 1) * 24 * 60 * 60
#=> 2024-10-07 15:57:31.39865918 +0000

Executed with Ruby 3.3.5