Ruby, check if date is a weekend?

后端 未结 7 1545
灰色年华
灰色年华 2021-02-02 08:51

I have a DailyQuote model in my rails application which has a date and price for a stock. Data in the database has been captured for this model

相关标签:
7条回答
  • 2021-02-02 09:58

    The simplest approach:

    today = Date.today
    
    if today.saturday? || today.sunday? 
      puts "Today is a weekend!"
    end
    

    You can also do this for any other day of the week. Ruby is fantastic and offers a lot of cool methods like this. I suggest when you get stumped take a look at what's available to the class by running .methods on it. So if you run Date.today.methods you will see these available.

    0 讨论(0)
提交回复
热议问题