Module: IfcDateClassMethods
- Included in:
- IfcDate
- Defined in:
- lib/ifc_date/class_methods.rb
Instance Method Summary collapse
- #is_leap_year?(year) ⇒ Boolean
-
#memoize(method_name) ⇒ Object
Do not execute computational expensive operations more than once.
Instance Method Details
#is_leap_year?(year) ⇒ Boolean
2 3 4 5 6 |
# File 'lib/ifc_date/class_methods.rb', line 2 def is_leap_year?(year) return false unless (year % 4).zero? return true unless (year % 100).zero? (year % 400).zero? end |
#memoize(method_name) ⇒ Object
Do not execute computational expensive operations more than once. NOTE: only use for explicitly non nil return methods, false is fine
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ifc_date/class_methods.rb', line 10 def memoize(method_name) unmemoized_name = "_unmemoized_#{method_name}" cache_key = method_name.end_with?('?') ? "is_#{method_name[0..-2]}" : "#{method_name}" alias_method unmemoized_name, method_name define_method method_name do |*args, &block| @_memoized_methods ||= {} return @_memoized_methods[cache_key] if @_memoized_methods.has_key?(cache_key) evaluated_result = send(unmemoized_name, *args, &block) @_memoized_methods[cache_key] = evaluated_result end end |