Skip to content

Instantly share code, notes, and snippets.

View tlowrimore's full-sized avatar

Tim Lowrimore tlowrimore

View GitHub Profile
@tlowrimore
tlowrimore / init.el
Created February 4, 2012 20:13
my emacs init.el
;; emacs configuration
(push "/usr/local/bin" exec-path)
(add-to-list 'load-path "~/.emacs.d")
(setq make-backup-files nil)
(setq auto-save-default nil)
(setq-default tab-width 2)
(setq-default indent-tabs-mode nil)
(setq inhibit-startup-message t)
@tlowrimore
tlowrimore / play.rb
Created March 1, 2012 00:26
Ruby Golf - Hole 3: Rock, Paper, Scisors
def play(m)
a,c=%w(Rock Scissors Paper),rand(4);[a[c%3],%w(Draw Lose Win)[a.index(m)-c]]*','
end
@tlowrimore
tlowrimore / text_layout.rb
Created March 9, 2012 17:14
Layout Plain-text with columns and shit. (Ruby 1.9)
# Quickly thrown together, but it works, and maybe it'll get YOU started on
# a better solution to the problem. Enjoy!
class TextLayout
attr_reader :page_width, :tab_width
def initialize(tab_width=4, page_width=80)
@tab_width, @page_width = tab_width, page_width
end
# Takes a title and an optional character which will be used
@tlowrimore
tlowrimore / gist:3842767
Created October 5, 2012 22:15
troublesome Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
user = ENV['OPSCODE_USER'] || ENV['USER']
base_box = "precise64"
# ------------------------------------------------------------------------------
# Short-hand node defs.
# ------------------------------------------------------------------------------
nodes = {
:eh_app_node => {
@tlowrimore
tlowrimore / union_scope.rb
Last active January 13, 2023 21:12
Unions multiple scopes on a model, and returns an instance of ActiveRecord::Relation.
module ActiveRecord::UnionScope
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def union_scope(*scopes)
id_column = "#{table_name}.#{primary_key}"
sub_query = scopes.map { |s| s.select(id_column).to_sql }.join(" UNION ")
where "#{id_column} IN (#{sub_query})"
@tlowrimore
tlowrimore / assets_gz.conf
Last active December 14, 2015 23:59
Apache config directives for serving Rails's precompiled .gz files, created by the asset pipeline during precompile.
<LocationMatch "^/assets/.*$">
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip
@tlowrimore
tlowrimore / or_scope.rb
Created March 27, 2013 19:39
OR's together provided scopes, returning an ActiveRecord::Relation. This implementation is smart enough to not only handle your _where_ clauses, but it also takes care of your joins! *Note* This is a work in progress, so it has room to grow
module ActiveRecord::OrScope
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def or_scope(*scopes)
conditions =
scopes
.map { |scope| "(#{scope.where_clauses.map{ |clause| "(#{clause})"}.join(" AND ")})" }
module Sluggo::Controller
MATCHER = /(^.+)-(.*$)/
def self.included(base)
base.send :include, InstanceMethods
end
module InstanceMethods
private
@tlowrimore
tlowrimore / vigenere.rb
Last active August 29, 2015 13:57
A Naive (Somewhat Readable) Vigenére Cipher
module Vigenere
def self.encrypt(key, phrase)
exec key, phrase
end
def self.decrypt(key, phrase)
exec key, phrase, -1
end
def self.exec(key, phrase, direction=1)
@tlowrimore
tlowrimore / palindrome.rb
Created March 12, 2014 03:33
Detects whether a number is palindromic without converting the number to a String.
module Palindrome
def self.palindrome?(n)
n == reverse(n)
end
def self.reverse(n)
rs = 0
while n > 0
n, r = n.divmod 10
rs = rs * 10 + r