tl = Time.now.localtime
(0..356).each {|i| d = (Date.new(tl.year, tl.month, tl.day) +i );
printf("\n%03s\n\n", Date::ABBR_MONTHNAMES[d.month]) if d.day == 1;
printf("%03s %02d.%02d\t\n", d.strftime("%a"), d.month, d.day);
printf("\n") if d.wday == 0;
}
Refactorings
No refactoring yet !
steenslag
February 27, 2010, February 27, 2010 20:49, permalink
The "unless (d+1).mday == 1" bit is to avoid two empty lines.
require 'date'
(Date.today .. Date.today>>12).each do |d|
print d.strftime("\n%b\n\n") if d.day == 1
puts d.strftime('%a %m.%d')
puts if d.wday.zero? unless (d+1).mday == 1
end
Martin Plöger
February 28, 2010, February 28, 2010 00:17, permalink
I'm using #inject to build an Array. That way you could use #compact to remove the empty lines (that's why I use nil instead of '') before calling #puts.
BTW: There's a leading empty line before the month-name on the first day of each month (should be removed maybe, but it was also there in your initial code... refactorings don't change behaviour ;-))
require 'date'
puts((Date.today..Date.today>>12).inject([]) do |m, d|
d.day == 1 ? m.push(nil, d.strftime('%b'), nil) : (m << nil if d.wday == 1)
m << d.strftime('%a %m.%d')
end)
I wrote the following to generate all the days of year in order to have a plain text linear planning calendar. I'm thinking it could be shorter.