57f954ba5409a6504af785ed8d42e009

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.

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 !

D41d8cd98f00b204e9800998ecf8427e

steenslag

February 27, 2010, February 27, 2010 20:49, permalink

No rating. Login to rate!

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
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

February 28, 2010, February 28, 2010 00:17, permalink

No rating. Login to rate!

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)

Your refactoring





Format Copy from initial code

or Cancel