Ruby, check if date is a weekend?

Viewed 26694

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 including weekends. The weekend price values have been set as 0.

I want to change all the weekend prices for Saturday and Sunday to whatever the price was on Friday. What is the best way to do this in Ruby? To identify if a date falls on a Sat or Sun and change its value to the Fri of that weekend?

7 Answers

Checking of weekend days (Saturday and Sunday) in the range of two dates in ruby

 weekend_days = [0,6]
 if (start_date.to_date..end_date.to_date).to_a.select {|k| weekend_days.include?(k.wday)}.present?
    # you code
 end
Related