Last Friday of each month: Difference between revisions

→‎{{header|Ruby}}: Use d -= (d.wday - 5) % 7
m (→‎{{header|C}}: remove leftover)
(→‎{{header|Ruby}}: Use d -= (d.wday - 5) % 7)
Line 331:
 
def last_friday(year, month)
# Find end of month = beginning of month + 1 month - 1 day.
if month == 12
d = Date.new(year+1, 1month, 1).>>(1) - 1
d -= (d.wday - 5) % 7 # Subtract days after Friday.
else
d = Date.new(year, month+1, 1)
end
begin
d -= 1
end until d.wday == 5 # Ruby 1.9: d.friday?
d
end
 
year = Integer(ARGV.shift)
(1..12).each {|month| puts last_friday(year, month)}</lang>
 
Friday is <code>d.wday == 5</code>; the expression <code>(d.wday - 5) % 7</code> counts days after Friday.
 
{{libheader|ActiveSupport}}
Anonymous user