Skip to content

Instantly share code, notes, and snippets.

@itchy
itchy / background.rb
Created June 18, 2014 04:44
poor man's background job
system("bash", "-c", "cd #{Rails.root} && bundle exec rake tasks:regenerate['#{current_practice.id}'] &")
@itchy
itchy / application_controller.rb
Last active December 21, 2015 23:39
Setting I18n localizations to format times based on user's timezone
class ApplicationController < ActionController::Base
around_filter :set_i18n_locale_from_current_user
def set_i18n_locale_from_current_user
org_timezone = I18n.current_timezone
if current_user
I18n.current_timezone = current_user.timezone
end
yield
ensure
@itchy
itchy / gist:5363607
Last active December 16, 2015 02:38 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts (Mac OS X)

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@itchy
itchy / intersection_spec.rb
Created February 27, 2013 02:09
Hack Club Traffic Light
class Light
attr_reader :state
def initialize()
@state = :red
end
def tick(count=1)
count.times do
@itchy
itchy / 0_start.rb
Created January 29, 2013 15:31
memoized 9 examples
#!/usr/bin/env ruby
class Routes
def distance(*segments)
calculate_distance(*segments)
end
private
def calculate_distance(*segments)
puts "Expensive Task"
@itchy
itchy / my_attr.rb
Created January 29, 2013 15:26
re-write attr_accessor with class_eval
#!/usr/bin/env ruby
require 'test/unit'
# instance_eval sets the current_class to the Eigen Class
# class_eval sets the current_class to the class
# both set self to the receiver
module MyAttr
def my_attr(*args)
args.each do |arg|
class_eval %{
@itchy
itchy / functional.rb
Created December 4, 2012 17:38
Best 2012 (c)
new_ssos = %w(va000123 va000316 va000469 va001190 va001974 va002175 va003344 va003376 va003927 va005733 va006048 va006092 va006186 va007015 va007386 va007526 va008636 va009657 va009667 va010103 va010239 va011367 va011632 va012062 va012345 va012637 va013178 va013235 va013259 va013299 va051200 va005202 va000271 va001092 va002041 va003249 va004225 va005256 va005394 va006165 va007083 va007269 va007415 va007606 va007822 va008375 va008376 va008541 va008573 va008585 va008600 va009097 va009354 va009627 va009983 va010010 va010020 va010026 va010030 va010050 va010056 va010211 va010290 va010392 va010431 va010910 va012015 va012962 va012964 va013319 va014186 va050907 va060769 va080884 va122590 va301922 va339626 va402002 va678910 va689722 va741236 va753780 va812009 va266649 va953068 va955819)
old_ssos = %w(dj000123 dj000316 dj000469 dj001190 dj001975 dj002175 dj003344 dj003376 dj003927 dj005733 dj006048 dj006092 dj006186 dj007015 dj007386 dj007526 dj008636 dj009657 dj009667 dj010103 dj010239 dj011367 dj011632 dj012062 dj01
@itchy
itchy / block.rb
Created December 4, 2012 17:32
Best 2012 (b)
valid_routes = add_segments(current_stops, starting_routes(start), stop) { |current, open_routes, matching_routes| current >= max_stops }
def add_segments(current_stops, routes, stop, matching_routes = [], &block )
# This method expects a block that receives the parameters current_stops, open_routes and matching_routes
# When the passed block evaluates to true
# it returns all of the routes found matching the start and stop locations
matching_routes << routes.select { |route| route.last_stop == stop }
matching_routes.compress!
@itchy
itchy / site_search.rb
Created December 4, 2012 17:19
Best 2012 (a)
class SiteSearch
extend LocalTire::ActiveModel::Extensions
per_page 10
search_indexes :profiles, :groups, :blog_posts, :questions, :chats
def self.search(params)
current_page = get_page(params[:page])
query_string = params[:query] if params.member?(:query)
search_indexes params[:scope] if params.member?(:scope) && params[:scope] != 'all'
@itchy
itchy / sample.rb
Created December 4, 2012 17:12
Worst 2012
module Sample
# SSJ 10-17-2012 this is a lot like try, but try was not working for me
# also it will try a collection of methods
# but does not take a block or additional parameters to pass to the method
def sample(*keys)
value = ""
keys.each do |key|
if self.respond_to?(key)
value = self.send key
break if value