Skip to content

Instantly share code, notes, and snippets.

View glv's full-sized avatar

Glenn Vanderburg glv

View GitHub Profile
@glv
glv / gist:134881
Created June 23, 2009 22:11 — forked from objo/gist:134796
entries.map(&:job).sort_by(&:number).map(&:project).compact.uniq
# Put this in your .irbrc, and then type
# "some_object.my_methods" in an IRB session
# for less noisy exploration of what objects
# can do.
class Object
def my_methods
base_object = case self
when Class then Class.new
when Module then Module.new
@glv
glv / gist:703997
Created November 17, 2010 20:16
USE INDEX with Active Record finders for Rails 3
# for background, see http://m.onkey.org/use-index-with-active-record-finders
# and http://twitter.com/#!/dougalcorn/status/4963159878139904
from = "#{quoted_table_name} USE INDEX(index_activities_on_created_at_and_country_id)"
Activity.from(from).where('created_at >= ? AND country_id = ?', 10.days.ago, 79).all
@glv
glv / gist:1438719
Created December 6, 2011 16:05 — forked from gvaughn/gist:1436816
Conway's Game of Life in functional ruby
def evolve(generation)
live_neighbor_stats = generation_stats(generation)
live_neighbor_stats[3] + (live_neighbor_stats[2] & generation)
end
def generation_stats(live_cells)
live_cells.each_with_object(Hash.new(0)) {|live_cell, counter|
neighbors(*live_cell).each {|neighbor| counter[neighbor] += 1 }
}.each_with_object(Hash.new {|h,k| h[k] = []}) {|(cell, count), collector| collector[count] << cell}
end
require 'active_record'
module ActiveRecord
module Associations
# This fix allows the association extension methods to work when used in combination
# with built-in Arel scoping methods (where, joins, etc.) and *also* with named scopes
# defined on the target class.
class AssociationProxy
def with_scope_with_extensions(*args, &block)
@glv
glv / git-git
Last active September 23, 2021 16:15
Save this in your path as "git-git" and then stop worrying about accidentally typing "git git something"
#!/bin/sh
exec "$GIT_EXEC_PATH/git" "$@"
@glv
glv / bundler-exec.sh
Created February 25, 2013 20:32
Smart aliases to wrap commands in "bundle exec" for you when needed
# I source this from ~/.bash_profile
# I think I got them from @JEG2
BUNDLED_COMMANDS="foreman rackup rails rake rspec ruby shotgun spec watchr nesta cap"
## Functions
bundler-installed()
{
which bundle > /dev/null 2>&1
@glv
glv / trouble-free-bundler.md
Last active March 16, 2018 14:50
This is a thing I wrote over a year ago for the internal LivingSocial wiki. There's nothing LS-specific about it, and it seems generally useful, so …

Trouble-Free Bundler

Every couple of weeks, I hear someone complaining about some difficulties with Bundler. Yesterday, it happened twice. But somehow I just never have those difficulties. I'm not saying Bundler is perfect; certainly in its early days it wasn't even close. But for the past two years it's been incredibly solid and trouble-free for me, and I think a large part of the reason is the way I use it. Bundler arguably does too much, and just as with Git, a big part of it is knowing what not to do, and configuring things to avoid the trouble spots.

@glv
glv / sort-without-threading-macro.clj
Created October 17, 2013 16:18
Avdi Grimm praised the symmetry of the Clojure version of line sorting (http://devblog.avdi.org/2013/10/16/sorting-lines-in-5-languages/). I thought it would be instructive to show how a more traditional Lispy version, without Clojure's ->> macro, obscures that symmetry.
(spit (nth *command-line-args* 2)
(clojure.string/join "\n"
(sort (clojure.string/split-lines (slurp (nth *command-line-args* 1))))))

Keybase proof

I hereby claim:

  • I am glv on github.
  • I am glv (https://keybase.io/glv) on keybase.
  • I have a public key whose fingerprint is 0CF3 D473 4F46 7494 1975 290C DA1F 2F76 9F2F 5496

To claim this, I am signing this object: