Skip to content

Instantly share code, notes, and snippets.

@moyashi
Created March 2, 2011 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moyashi/850720 to your computer and use it in GitHub Desktop.
Save moyashi/850720 to your computer and use it in GitHub Desktop.
ゴミの日を返すDateTime::kind_of_separated_refuse
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# kind of separated refuse
# written by moyashi
# 2011.03.02
require 'date'
# grabbed from http://rubyist.g.hatena.ne.jp/yamaz/20070125
class Object
def enum(*list)
mc = Module === self ? self : self.class
list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, i}
end
def bit_enum(*list)
mc = Module === self ? self : self.class
list.flatten.each_with_index{|e, i| mc.const_set e.to_s.intern, 2 ** i}
end
end
class DateTime
enum %w(SUN MON TUE WED THU FRI SAT)
# N = NONE
# K = KANEN
# F = FUNEN
# S = SHIGEN
# P = PET
enum %w(N K F S P)
enum %w(SHORT LONG SLONG)
LABELS = []
LABELS[N] = ["無", "無し", "回収無し"]
LABELS[K] = ["可", "可燃", "可燃ごみ"]
LABELS[F] = ["不", "不燃", "不燃ごみ"]
LABELS[S] = ["資", "資源", "資源ごみ"]
LABELS[P] = ["P", "PET", "PETボトル"]
# 各行は書くまでもなく曜日別、曜日の中に各週の分別ゴミの種別を配列として入れる
# うちの近所では第一第三水曜は不燃、第二第四水曜はペットボトルといった具合なので
# 「月曜は週を問わず資源ゴミ」という場合は、四つすべて同じく「資源」を入れておく
SEPARATED_REFUSES = {
SUN => [N, N, N, N],
MON => [S, S, S, S],
TUE => [K, K, K, K],
WED => [F, P, F, P],
THU => [N, N, N, N],
FRI => [K, K, K, K],
SAT => [N, N, N, N]
}
# 月の何週目か
def mweek
return self.cweek - (DateTime.new(self.year, self.mon).cweek - 1)
end
# 分別ゴミの種別を文字列で返す
# 翌日朝のゴミの種別を告知する目的で、半日前倒しでゴミの種別を決定
# 日曜日の正午から月曜日の正午まで月曜の資源ゴミを返し
# 月曜日の正午以降は火曜日の可燃ゴミを返すといった具合
def kind_of_separated_refuse(format)
# 半日後
halfAfterDay = self + 0.5
return LABELS[SEPARATED_REFUSES[halfAfterDay.wday][halfAfterDay.mweek - 1]][format]
end
end
puts DateTime.now.kind_of_separated_refuse(DateTime::LONG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment