Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added pattern generator to the README file.

  • Loading branch information...
commit ed881307323747c5b28f9270a637f674f88fac65 1 parent cb6d636
@jhubert authored
Showing with 30 additions and 8 deletions.
  1. +19 −1 README
  2. +11 −7 notes.txt
View
20 README
@@ -51,4 +51,22 @@ Example: Date.today.recur(:yearly, "2:100001000001") # Every 2 years in January,
Example: Date.today.recur(:yearly, "1:110000000000:001000:1000000") # Every year on the 3rd Sunday of January and February
Written by Jeremy Hubert
-http://jeremyhubert.com
+http://jeremyhubert.com
+
+
+**** Other Tips? ****
+
+This method will generate the binary pattern for a set of numbers or a single integer
+
+def pattern_from_numbers(number_array=[], options={})
+ options = { :convert_to_binary => true, :base_zero_numbers => false }.merge(options)
+ number_array = [number_array] if number_array.is_a?(Integer)
+ base_fix = options[:base_zero_numbers] ? 0 : 1
+ number_array = number_array.collect { |x| 2**(x-base_fix) } if options[:convert_to_binary]
+ number_array.inject(0) { |sum,x| sum + x }.to_s(base=2).reverse
+end
+
+puts pattern_from_numbers([3,16,31]) # the third, 16th and 31st day of the month
+puts pattern_from_numbers(3) # third
+puts pattern_from_numbers([1,8,64], :convert_to_binary => false) # numbers passed in binary format already
+puts pattern_from_numbers(Date.today.cwday, :base_zero_numbers => true) # don't subtract 1 from all numbers because they already start at 0 instead of 1
View
18 notes.txt
@@ -12,13 +12,17 @@ def check(dates)
end
# Binary representation of days of the week
-1 1 Saturday
-2 2 Friday
-4 3 Thursday
-8 4 Wednesday
-16 5 Tuesday
-32 6 Monday
-64 7 Sunday
+0 1 Saturday
+1 2 Friday
+2 3 Thursday
+4 4 Wednesday
+8 5 Tuesday
+16 6 Monday
+32 7 Sunday
+
+
+
+
require 'date'
class Date
Please sign in to comment.
Something went wrong with that request. Please try again.