Skip to content

Instantly share code, notes, and snippets.

class User < ActiveRecord::Base
scope :in_time_zones, -> { |zones| where(time_zone_name: zones) }
def self.unique_time_zone_names
select("DISTINCT time_zone_name").pluck(:time_zone_name).compact
end
end
require 'TZInfo'
class TimeZoneSearch
def initialize(zones_to_search)
@zones_to_search = zones_to_search
end
def zones_with_local_hour(hour_to_match, utc_time = Time.now.utc.to_datetime)
zone_matches = []
utc_minute = utc_time.minute
require 'TZInfo'
today = Time.now
(0..23).each do |hour|
(0..45).each do |minute|
utc_time_to_check = Time.utc(today.year, today.month, today.day, hour, minute, 0)
TZInfo::Timezone.all_identifiers.each do |zone_name|
local_tz = TZInfo::Timezone.get(zone_name)
local_time = local_tz.utc_to_local(utc_time_to_check).to_datetime
if local_time.minute == 0 && local_time.hour == 8
class Matrix
def initialize(numbers)
@numbers = numbers
end
def rows
@rows ||= @numbers.split("\n").map{ |row| row.split.map(&:to_i) }
end
def columns
# Search all zones for 8:00am.
(0..23).each do |hour|
[0, 15, 30, 45].each do |minute|
utc_time_to_check = Time.utc(2014, 6, 28, hour, minute, 0)
TZInfo::Timezone.all_identifiers.each do |zone_name|
local_tz = TZInfo::Timezone.get(zone_name)
local_time = local_tz.utc_to_local(utc_time_to_check).to_datetime
if local_time.minute == 0 && local_time.hour == 8
puts utc_time_to_check.strftime("%H:%M") + ' UTC => ' + local_time.strftime("%H:%M") + ' in ' + zone_name
end
class Pet < ActiveRecord::Base
as_enum :species, {:cat => 0, :dog => 1, :other => 2}, :prefix => true
as_enum :status, {:lost => 0, :found => 1}, :prefix => true
validates :name, :description, :zip, :species_cd, presence: true
validates :zip, numericality: { only_integer: true}
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
# username@Machine ~/dev/dir[master]$ # clean working directory
# username@Machine ~/dev/dir[master*]$ # dirty working directory
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
Person = function(name) {
this.name = name;
}
var thomas = new Person('Thomas');
var amy = new Person('Amy');
Person.prototype.getName = function() {
return this.name;
}
Function.prototype.cache = function(){
var _cache = {};
// var key = function(args) {
// return 'x' + args.join('');
// }
var key = function(args) {
var sig = 'x', index = args.length;
while (index--) {
Function.prototype.cache = function(){
var _cache = {};
var key = function(args) {
var sig = 'x', index = args.length;
while (index--) {
sig += args[index].toString();
}
return sig;
}