Last active
August 29, 2015 13:57
-
-
Save taesiri/9712675 to your computer and use it in GitHub Desktop.
Print Calendar in Console like cal command in Mac OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalendarPrinter | |
@@daysOfWeek = ['Su','Mo', 'Tu','We','Th','Fr', 'Sa' ] | |
def initialize(monthName, year, startDayOfMonth, daysOfMonth) | |
@month = monthName | |
@year = year | |
@firstDayOfMonth = startDayOfMonth | |
@totalDaysOfMonth = daysOfMonth | |
end | |
def to_s | |
puts (@month + " " + @year.to_s).center((@@daysOfWeek.join(' ').length)) | |
puts @@daysOfWeek.join(' ') | |
fdayIndex = @@daysOfWeek.index(@firstDayOfMonth) | |
print ' ' * fdayIndex | |
(1..@totalDaysOfMonth).each_with_index do |day, index| | |
print (day).to_s.rjust(2) + ' ' | |
if (index+fdayIndex+1) % 7 == 0 | |
print "\n" | |
end | |
end | |
print "\n" | |
end | |
end | |
m = CalendarPrinter.new('March', 2014, 'Sa' ,31) | |
m.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment