Last active
March 22, 2020 03:34
-
-
Save hechien/1b4ac6b6b7c8d22596597eec8cf7e6dc to your computer and use it in GitHub Desktop.
用 Ruby 寫的小東西
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
#!/usr/bin/env ruby | |
# 五鼠遁起法 | |
class GanZhiTable | |
TIAN_GAN = "甲乙丙丁戊己庚辛壬癸".split("").freeze | |
DI_ZHI = "子丑寅卯辰巳午未申酉戌亥".split("").freeze | |
def tian_gan(index) | |
index = index - TIAN_GAN.size if index >= TIAN_GAN.size | |
TIAN_GAN[index] | |
end | |
def tian_gan_index(tian_gan) | |
TIAN_GAN.find_index(tian_gan) | |
end | |
def di_zhi(index) | |
index = index - DI_ZHI.size if index >= DI_ZHI.size | |
DI_ZHI[index] | |
end | |
def di_zhi_index(di_zhi) | |
DI_ZHI.find_index(di_zhi) | |
end | |
end | |
class HourGanZhi | |
attr_reader :full_gan_zhi | |
def initialize(year_gan, hour) | |
@year_gan = year_gan | |
@hour = hour - 1 | |
@table = GanZhiTable.new | |
calculate | |
end | |
def calculate | |
first_hour_tian_gan = case @year_gan | |
when "甲", "己" then "甲" | |
when "乙", "庚" then "丙" | |
when "丙", "辛" then "戊" | |
when "丁", "壬" then "庚" | |
else "壬" | |
end | |
hour_gan_offset = @table.tian_gan_index(first_hour_tian_gan) | |
tian_gan_number = hour_gan_offset + @hour | |
@full_gan_zhi = "#{@table.tian_gan(tian_gan_number)}#{@table.di_zhi(@hour)}" | |
end | |
end | |
t = GanZhiTable.new | |
t.tian_gan((1 - 1)) #=> 甲 | |
t.tian_gan((5 - 1)) #=> 戊 | |
t.tian_gan((10 - 1)) #=> 癸 | |
t.tian_gan((13 - 1)) #=> 丙 | |
t.tian_gan((14 - 1)) #=> 丁 | |
t.di_zhi((1 - 1)) #=> 子 | |
t.di_zhi((4 - 1)) #=> 卯 | |
t.di_zhi((8 - 1)) #=> 未 | |
t.di_zhi((13 - 1)) #=> 子 | |
puts HourGanZhi.new("己", 1).full_gan_zhi | |
puts HourGanZhi.new("己", 3).full_gan_zhi | |
puts HourGanZhi.new("己", 5).full_gan_zhi | |
puts HourGanZhi.new("戊", 1).full_gan_zhi | |
puts HourGanZhi.new("戊", 3).full_gan_zhi | |
puts HourGanZhi.new("戊", 5).full_gan_zhi |
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
#!/usr/bin/env ruby | |
# 五虎遁起法 | |
class MonthGanZhi | |
TIAN_GAN = "甲乙丙丁戊己庚辛壬癸".split("") | |
DI_ZHI = "寅卯辰巳午未申酉戌亥子丑".split("") | |
attr_reader :full_gan_zhi | |
def initialize(year_gan, month) | |
@year_gan = year_gan | |
@month = month - 1 | |
calculate | |
end | |
def calculate | |
first_month_tian_gan = case @year_gan | |
when "甲", "己" then "丙" | |
when "乙", "庚" then "戊" | |
when "丙", "辛" then "庚" | |
when "丁", "壬" then "壬" | |
else "甲" | |
end | |
month_gan_offset = TIAN_GAN.find_index(first_month_tian_gan) | |
tian_gan_number = month_gan_offset + @month | |
@full_gan_zhi = "#{TIAN_GAN[tian_gan_number]}#{DI_ZHI[@month]}" | |
end | |
end | |
puts MonthGanZhi.new("己", 1).full_gan_zhi | |
puts MonthGanZhi.new("己", 3).full_gan_zhi | |
puts MonthGanZhi.new("己", 5).full_gan_zhi | |
puts MonthGanZhi.new("戊", 1).full_gan_zhi | |
puts MonthGanZhi.new("戊", 3).full_gan_zhi | |
puts MonthGanZhi.new("戊", 5).full_gan_zhi |
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
#!/usr/bin/env ruby | |
class YearGanZhi | |
TIAN_GAN = "甲乙丙丁戊己庚辛壬癸".split("") | |
DI_ZHI = "子丑寅卯辰巳午未申酉戌亥".split("") | |
attr_reader :full_gan_zhi | |
def initialize(year, western_year = false) | |
@year = year | |
@year -= 1911 if western_year == true | |
@full_gan_zhi = "" | |
calculate | |
end | |
def calculate | |
tian_gan_number = @year - @year.truncate(-1) - 3 | |
di_zhi_number = @year % 12 - 1 | |
@full_gan_zhi = "#{TIAN_GAN[tian_gan_number]}#{DI_ZHI[di_zhi_number]}" | |
end | |
end | |
puts YearGanZhi.new(73).full_gan_zhi | |
puts YearGanZhi.new(77).full_gan_zhi | |
puts YearGanZhi.new(81).full_gan_zhi | |
puts YearGanZhi.new(5).full_gan_zhi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment