Created
April 2, 2009 06:57
-
-
Save morimori/89063 to your computer and use it in GitHub Desktop.
Date と Time に直前と直後の各曜日を取得するメソッドを追加
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
class Date | |
module Extender | |
module LastAndNext | |
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'].each_with_index do |weekday, index| | |
define_method "last_#{weekday}" do | |
return self if wday == index | |
offset = wday - index | |
self - (offset < 0 ? offset + 7 : offset) | |
end | |
define_method "next_#{weekday}" do | |
return self if wday == index | |
offset = wday - index | |
self + (7 - (offset < 0 ? offset + 7 : offset)) | |
end | |
end | |
end | |
end | |
include Extender::LastAndNext | |
end |
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
class Time | |
module Extender | |
module LastAndNext | |
['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'].each_with_index do |weekday, index| | |
define_method "last_#{weekday}" do | |
return self if wday == index | |
offset = wday - index | |
self - (offset < 0 ? offset + 7 : offset) * 86400 | |
end | |
define_method "next_#{weekday}" do | |
return self if wday == index | |
offset = wday - index | |
self + (7 - (offset < 0 ? offset + 7 : offset)) * 86400 | |
end | |
end | |
end | |
end | |
include Extender::LastAndNext | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment