mirror of
https://github.com/jimeh/time_ext.git
synced 2026-02-19 13:26:39 +00:00
Define dynamic iteration methods in a faster and
cleaner way.
This commit is contained in:
@@ -55,12 +55,13 @@ class Time
|
||||
end
|
||||
|
||||
# Dynamically define convenience methods, like #each_hour instead of #each(:hour).
|
||||
[:year, :month, :day, :hour, :min, :minute, :sec, :second].each do |unit|
|
||||
[:year, :month, :day, :hour, :min, :sec].each do |unit|
|
||||
[:each, :beginning_of_each, :map_each, :map_beginning_of_each].each do |method|
|
||||
called_unit = (unit == :minute) ? :min : (unit == :second) ? :sec : unit
|
||||
define_method "#{method}_#{unit}" do |*args, &block|
|
||||
send(method, called_unit, *args, &block)
|
||||
send(method, unit, *args, &block)
|
||||
end
|
||||
class_eval { alias :"#{method}_minute" :"#{method}_min" } if unit == :min
|
||||
class_eval { alias :"#{method}_second" :"#{method}_sec" } if unit == :sec
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user