Skip to content

Instantly share code, notes, and snippets.

@alvinsj
Created March 14, 2012 12:38
Show Gist options
  • Save alvinsj/2036201 to your computer and use it in GitHub Desktop.
Save alvinsj/2036201 to your computer and use it in GitHub Desktop.
Mask days of week into binary, save to integer field
DAYS = %w[0 1 2 3 4 5 6 7]
DAYS_HUMAN = %w[Sun Mon Tue Wed Thur Fri Sat]
def days=(days)
self.days_mask = (days & DAYS).map { |r| 2**DAYS.index(r) }.sum
end
def days
DAYS.reject do |r|
((days_mask || 0) & 2**DAYS.index(r)).zero?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment