Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:27:41+00:00 2026-06-11T02:27:41+00:00

I’m pretty new to Ruby and I’m trying to extend the Date class to

  • 0

I’m pretty new to Ruby and I’m trying to extend the Date class to add a function that will return the first sunday of the month. This is to help draw a monthly calendar. So say September begins on Saturday. Then i’d like this function to return August 26, as that is the date the calendar will need to draw first.

I have this working in irb as:

fs = Date.today - Date.today.mday + 1
fs = fs - fs.wday

So now i’m trying to extend the date class as

class Date
  def first_of_month
    return Date.new(self.year, self.month, 1)
  end

  def first_sunday_of_first_week
    return self.first_of_month - self.wday
  end
end

The first of the month method seems to work fine.
The first sunday of the month doesnt. i’m thinking it has something to do with Date.new missing but no matter how I’ve tried to redo this it doesn’t seem to work. I’m making a Sinatra app so I dont have the rails functions that do this.

The function here returns the first of the month for both methods and is not returning the first sunday of the month.

How do I correctly get this extended method to work? Thanks

UPDATE:

class Date
  def first_of_month
    return Date.new(self.year, self.month, 1)
  end

  def first_sunday_of_first_week
    first_of_month - first_of_month.wday
  end
end

This seems to work but is it correct or am i forcing more work than needed. Does this not created to Date.new’s to return one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:27:42+00:00Added an answer on June 11, 2026 at 2:27 am

    You don’t need the self.method and you don’t need the return either, you can just write method to make it more concise.
    The problem is that you just subtract the wday of the current date, but you need to subtract the wday of the first day of the month

    def first_sunday_of_first_week
      first_of_month - first_of_month.wday
    end
    

    That worked for me.

    To your update: You could make it twice as efficient by avoiding the first_of_month method and doing it the following way instead:

    def first_sunday_of_first_week
        first = Date.new(year, month, 1)
        first - first.wday
    end
    

    Your method calls the first_of_month method twice, hence the date gets created two times and in my version, only one instance is created. If performance is an issue, this would be slightly more efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.