Skip to content

Instantly share code, notes, and snippets.


David Brady dbrady

View GitHub Profile
View be_matcher.rb
# What is existence?
class Be
def initialize
def matches?(actual)
defined? actual
View bash_session.txt
dbrady@robomonkey:~/lmp/market (sprint)$ ruby -e 'require "rubygems"; require "trollop"; puts Trollop.class'
dbrady@robomonkey:~/lmp/market (sprint)$ ruby -e 'require "rubygems"; require "nokogiri"; puts Nokogiri.class'
dbrady@robomonkey:~/lmp/market (sprint)$ cd
dbrady@robomonkey:~ $ ruby -e 'require "rubygems"; require "trollop"; puts Trollop.class'
dbrady@robomonkey:~ $ ruby -e 'require "rubygems"; require "nokogiri"; puts Nokogiri.class'
./nokogiri.rb:4: uninitialized constant Nokogiri (NameError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
View nokotest.rb
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML.parse(<<-eohtml)
View gist:64170
>> class String
>> alias :/ :split
>> end
=> nil
>> "foo"///
=> ["f", "o", "o"]
View gist:181256
$ cat bin/localhost
open http://localhost/$*
dbrady / gist:673524
Created Nov 12, 2010
Interleaved Unpacking...
View gist:673524
# This takes about 2s for a 53Mb file.
bytes = audio_file.read_into(audio_buffer)
# This takes less than a second, BUT the data is interleaved
# (left, right, left, right, left, right, etc)
self.audio_data = audio_buffer.unpack('s*')
# Less than a second.
self.left_channel = NArray.float(2, audio_data.size)
self.right_channel = NArray.float(2, audio_data.size)
View gist:783742
describe "Server" do
describe "methods" do
let(:put) { "put foobar beans 5\nhowdy" }
it "should be valid for a valid put method" do
# before all
@pid = spawn("bin/server")
# before each
@sock = "", 3000
View gist:786710
# One pattern I use so much I need to make an editor macro for it:
class Foo
attr_accessor :x, :y, :z, :w
# --> macro on above line should produce:
def initialize(x, y, z, w)
@x, @y, @z, @w = x, y, z, w
# --> end macro
dbrady / hanging_if_expression.rb
Created Feb 26, 2011
Semantically identical code, three idiomatic ways of expressing it
View hanging_if_expression.rb
# Coders from from C often like this style:
log status == OK ? "OK" : "PROBLEM"
log "All done."
# But since if returns a value in ruby, you can spell it out:
log if status == OK
dbrady / node_js_prepended_comma.js
Created Mar 3, 2011
Example of how the Node.js community prepends commas
View node_js_prepended_comma.js
// Consider the humble object:
var dave = {
name: "Dave",
age: 39,
color: "green"
// If I add to this and forget to add another trailing comma, it's a
// syntax error:
var dave = {
You can’t perform that action at this time.