Skip to content

Instantly share code, notes, and snippets.

View larsthegeek's full-sized avatar

James Peterson larsthegeek

  • Total Tech Shop
View GitHub Profile
(defn optify
"Helper that examines paths with the supplied prefix and either subs
in their cache-busting URLs or returns them unchanged."
[req prefix]
(fn [^String src]
(or (and (.startsWith src prefix)
(not-empty (link/file-path req src)))
src)))

Eliminating Code Smell With Grunt

Gvn Lazar Suntop @gvn

Intro

I love clean code. There, I said it. I pride myself on passing strict linting standards and keeping my code easy to read. It's not just a personal proclivity, but a choice I hope benefits other developers.

My general experience with teams has been that code style is something people care about and have strong personal preferences. Typically, at some point people get tired of dealing with inconsistency and a standardization meeting is called. This is, of course, an important discussion to have. The problem that tends to occur is either lack of documentation or lack of enforcement of the agreed upon style. Additionally, new team members or contributors may not have access to a clear set of rules.

Beyond the challenge of defining rules lies the supreme annoyance of enforcing them. Code reviews become cluttered with nits to be picked. Time is wasted. The solution I settled on was simply automating the conformanc

#!/usr/bin/perl -w
#
# splitmysqldump - split mysqldump file into per-database dump files.
use strict;
use warnings;
my $dbfile;
my $dbname = q{};
my $header = q{};
while (<>) {
/var/log/nginx_*.log {
daily
compress
delaycompress
rotate 2
missingok
nocreate
sharedscripts
postrotate
test ! -f /var/run/nginx.pid || kill -USR1 `cat /var/run/nginx.pid`
@larsthegeek
larsthegeek / time_diff.rb
Created February 13, 2013 20:14
A better function for producing a nice time diff
# produces [year, month, day, hour, minute, second]
# from time is the older date
def time_diff(from_time, to_time)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
components = []
%w(year month day hour minute second).map do |interval|
distance_in_seconds = (to_time.to_i - from_time.to_i).round(1)
delta = (distance_in_seconds / 1.send(interval)).floor
delta -= 1 if from_time + delta.send(interval) > to_time
@larsthegeek
larsthegeek / time_diff_in_natural_language.rb
Created February 13, 2013 19:58
Nice fx to get a time span in natural language
# from http://stackoverflow.com/questions/1065320/in-rails-display-time-between-two-dates-in-english
def time_diff_in_natural_language(from_time, to_time)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_seconds = ((to_time - from_time).abs).round
components = []
%w(year month week day).each do |interval|
# For each interval type, if the amount of time remaining is greater than
# one unit, calculate how many units fit into the remaining time.
(defparameter *valid-chars* '(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z))
(defparameter *table* nil)
(defparameter *norm-table* nil)
(defun build-table ()
(setf *table* (make-hash-table))
(with-open-file (in #p"corpus.txt" :direction :input)
(loop with lower-c with c with p while (not (eql c :eof)) do
(setf c (read-char in nil :eof)

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.

@larsthegeek
larsthegeek / gist:4063239
Created November 13, 2012 01:17
Open all files in quickfix window
" quickfixopenall.vim
"Author:
" Tim Dahlin
"
"Description:
" Opens all the files in the quickfix list for editing.
"
"Usage:
" 1. Perform a vimgrep search
" :vimgrep /def/ *.rb
@larsthegeek
larsthegeek / gist:4029013
Created November 7, 2012 01:39
Update RubyGems to a specific version (for legacy system installs)
How to update RubyGems to a specific version
RubyGems can update itself to the latest version:
gem update --system
However, you always get the newest version. This may be a problem if you’re trying to replicate a known-good environment: for example, the output format of `gem list` in RubyGems 1.3.7 breaks Chef 0.8.16 in certain circumstances. 1.3.6 is fine.
Here’s how to update to a specific version—in this case, 1.3.6:
gem install -v 1.3.6 rubygems-update && ruby `gem env gemdir`/gems/rubygems-update-1.3.6/setup.rb