Skip to content

Instantly share code, notes, and snippets.

@taesiri
Last active August 29, 2015 13:57
Show Gist options
  • Save taesiri/9712675 to your computer and use it in GitHub Desktop.
Save taesiri/9712675 to your computer and use it in GitHub Desktop.
Print Calendar in Console like cal command in Mac OS X
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