借りたので、一通り練習問題実施中。
(3)Dateクラスを使って今月の1日と月末の日付と曜日を求め、次のような形式で カレンダーを表示させてください。 2010 Mar Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
ソース
require 'date' def cal(year, month) first = Date.new(year, month, 1) last = (first >> 1) - 1 days = (1..last.day).map days = Array.new(first.wday, "").concat(days) puts first.strftime(" %Y %b") puts "Su Mo Tu We Th Fr Sa" days.each_slice(7) do |week| puts week.map{|day| "%2s" % day }.join(" ") end end cal(2010, 3) cal(2011, 4)
補足
last = (first >> 1) - 1
この月の加減する>>って知らんかった。