トップ 最新

GREEN HARMONY 手記

2018年01月08日 40日暦(ruby) [長年日記]

40日暦

cal40.rb

require 'date'


def cal40 date
  ary = []
  (1 .. 9).each {|mm|
    max = 
    if mm.odd?
      41
    else
      if mm == 2 and date.leap?
        41
      else
        40
      end
    end
    
    (1 .. max).each {|dd|
      ary << [mm,dd]
    }
  }
  ary
end

light_next.rb

def next1 ary
  
  h = {2=>4,
    4=>6,
    6=>2,
    3=>5,
    5=>7,
    7=>3,
    1=>8,
    8=>9,
    9=>1
  }
  n3 = h[ary[ary.length-1]];
  [ary[0],to9(9+(n3-ary[0])),n3]

end

def to9 n
  result = n % 9
  if result == 0 then 9; else  result end

end

4.rb (6人の計算の表示)

require './cal40'
require './light_next'

ary = nil

aDate =
	(if ARGV[0]
		Date.parse(ARGV[0])
	else 
		Date.today
	end)

ary1 = []
ary2 = []

n1 = to9(aDate.year)
n2 = to9(aDate.month+aDate.day)
n3 = to9(n1 + n2)
ary1 << [n1,n2,n3]
ary1 << next1(ary1.last)
ary1 << next1(ary1.last)

aCal40 = cal40 aDate

lightmd = aCal40[aDate.yday-1]
n22 = to9(lightmd[0] + lightmd[1])
n23 = to9(n1 + n22)

ary2 << [n1,n22,n23]
ary2 << next1(ary2.last)
ary2 << next1(ary2.last)

print aDate,"\n"

ary3 = ary1.zip(ary2).flatten(1)
ary3.each_with_index {|elm,i|
  case i
  when	0
		print "◎"
  when  1
  		print "○"
  else
      	print " "  
  end
  print elm.join,"\n"
}


実行

% ruby 4.rb

2018-01-08
◎292
○292
 224
 224
 246
 246


プロを目指す人のためのRuby入門 言語仕様からテスト駆動開発・デバッグ技法まで Software Design plus