GREEN HARMONY 手記
2020年04月11日 Rubyでプログラミング20200411 [長年日記]
☆ Ruby でプログラミング
今日の光暦(40日暦)と宇宙暦を表示
hikari.rb
# coding: utf-8 def hikari n a = n % 9 if a == 0 9 else a end end $name = {1=>'ちり',2=>'あか',3=>'オレンジ',4=>'きい',5=>'みどり', 6=>'あお',7=>'あい',8=>'むらさき',9=>'みよ'} $name_en = { 1=>'dust (amethyst)', 2=>'red', 3=>'orange', 4=>'yellow', 5=>'green', 6=>'blue', 7=>'indigo', 8=>'purple', 9=>'look (olive)' }
cal40.rb (40日暦)
# -*- coding: utf-8 -*- 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
2.rb (表示するプログラム)
# coding: utf-8 require 'date' require './cal40' require './hikari' aDate = Date.today print aDate.iso8601,"\n" ary = cal40 aDate lightmd = ary[aDate.yday-1] print "光暦 (40日暦) #{lightmd[0]}月#{lightmd[1]}日\n" mon1 = hikari(aDate.month) day1 = aDate.day if day1 < 10 day1 += 90 else #if (day1 % 10) == 0 # day1 += 9 #end if (day1 % 10) == 0 day1 = (day1 / 10) + 90 end end print "宇宙暦 " print [mon1,day1].join,"\n"
実行
% ruby 2.rb 2020-04-11 光暦 (40日暦) 3月20日 宇宙暦 411