[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Days in month

Peña, Botp

4/28/2005 6:20:00 AM

Ara.T.Howard@noaa.gov [mailto:Ara.T.Howard@noaa.gov] wrote:

# harp:~ > cal 2 2005
# February 2005
# 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
#
# harp:~ > cal 12 2005
# December 2005
# 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
#
#boy i love ruby.

i took a deep breath there.
I thought you were going to show us your cal.rb :-)

A ruby quiz perhaps?

kind regards -botp

#
#cheers.
#
#-a
#--


2 Answers

Logan Capaldo

4/29/2005 12:43:00 AM

0

On 4/28/05, "Peña, Botp" <botp@delmonte-phil.com> wrote:
> Ara.T.Howard@noaa.gov [mailto:Ara.T.Howard@noaa.gov] wrote:
>
> # harp:~ > cal 2 2005
> # February 2005
> # 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
> #
> # harp:~ > cal 12 2005
> # December 2005
> # 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
> #
> #boy i love ruby.
>
> i took a deep breath there.
> I thought you were going to show us your cal.rb :-)
>
> A ruby quiz perhaps?
>
> kind regards -botp
>
> #
> #cheers.
> #
> #-a
> #--
>
>

Well here's mine:

#!/usr/bin/env ruby
require 'date'
month = ARGV[0] ? ARGV[0].to_i : Date.today.mon
year = ARGV[1] ? ARGV[1].to_i : Date.today.year

month_names = %w{January February March April May June July August September
October November Decemeber}

dow_banner = %{Su Mo Tu We Th Fr Sa}
days_in_mon = Date.civil(year, month, -1).day

# Print title:
print " " * ((dow_banner.length -
(year.to_s.length + 1 + month_names[month - 1].length)) / 2)
print "#{month_names[month - 1]} #{year}\n"
print "#{dow_banner}\n"

dow_counter = Date.civil(year, month, 1).wday

print " " * 3 * dow_counter

(1..days_in_mon).each { |i|
if i < 10
print " #{i}"
else
print "#{i}"
end
print " "
dow_counter += 1
if dow_counter % 7 == 0
print "\n"
end
}
print "\n"



Kent Sibilev

4/29/2005 3:09:00 AM

0

Someting like that:

require 'date'
require 'enumerator'

class Date
def days_in_month
self.class.civil(year, month, -1).day
end
alias dim days_in_month

def weekdays
(1..dim).to_a
end

def cal_print
banner = ABBR_DAYNAMES.map{|d| d[0..1]}.join(' ')

puts "#{MONTHNAMES[month]} #{year}".center(banner.size)
puts banner
wd = [' '] * wday + weekdays
wd.each_slice(7) do |slice|
puts slice.map{|d| d.to_s.rjust(2)}.join(' ')
end
end
end

month = (ARGV[0] || Date.today.mon).to_i
year = (ARGV[1] || Date.today.year).to_i

Date.civil(year, month).cal_print

Kent.

On 4/28/05, "Peña, Botp" <botp@delmonte-phil.com> wrote:
> Ara.T.Howard@noaa.gov [mailto:Ara.T.Howard@noaa.gov] wrote:
>
> # harp:~ > cal 2 2005
> # February 2005
> # 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
> #
> # harp:~ > cal 12 2005
> # December 2005
> # 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
> #
> #boy i love ruby.
>
> i took a deep breath there.
> I thought you were going to show us your cal.rb :-)
>
> A ruby quiz perhaps?
>
> kind regards -botp
>
> #
> #cheers.
> #
> #-a
> #--
>
>