Skip to content

Instantly share code, notes, and snippets.

View ernie's full-sized avatar

Ernie Miller ernie

View GitHub Profile
@ernie
ernie / 3-0-bench.txt
Created August 22, 2011 18:40
AR serialization memory usage
====================
3.0.10
====================
user system total real
============================================================
RSS : 37592k (37592k)
Objects : 182328 (182328)
============================================================
all 0.650000 0.010000 0.660000 ( 0.682556)
============================================================
@ernie
ernie / results.txt
Created August 24, 2011 00:04
Script used to benchmark Valium
============================================================
Benchmarking with Ruby 1.9.3 and ActiveRecord 3.2.0.beta
============================================================
user system total real
============================================================
RSS : 31432k (31432k)
Objects : 102195 (102195)
============================================================
SINGLE-VALUE TEST
@ernie
ernie / to_ary.rb
Created August 26, 2011 17:55
:to_ary fun
# If you put a DSL stub object like this inside an array and call array#flatten without
# being sure to call super on :to_ary, you get fun stuff like this:
# can't convert Squeel::Nodes::Stub to Array
# (Squeel::Nodes::Stub#to_ary gives Squeel::Nodes::KeyPath)
def method_missing(method_id, *args)
super if method_id == :to_ary
if args.empty?
KeyPath.new(self, method_id)
elsif (args.size == 1) && (Class === args[0])
KeyPath.new(self, Join.new(method_id, Arel::InnerJoin, args[0]))
@ernie
ernie / wth_mysql.rb
Created August 30, 2011 20:22
MySQL query planner fail.
#!/usr/bin/env ruby
#
# $ ./wth_mysql.rb
# user system total real
# mysql 0.020000 0.010000 0.030000 ( 0.617876)
# sqlite 0.000000 0.000000 0.000000 ( 0.001234)
# postgresql 0.000000 0.000000 0.000000 ( 0.002423)
srand 123456789
require 'active_record'
@ernie
ernie / lies.rb
Created September 6, 2011 14:11
instance_exec LIES!!!
#!/usr/bin/env ruby
# http://www.ruby-doc.org/core-1.8.7/classes/Object.html#M000006 :
#
# obj.instance_exec(arg...) {|var...| block } => obj
#
# Executes the given block within the context of the receiver (obj).
# In order to set the context, the variable self is set to obj while
# the code is executing, giving the code access to obj‘s instance variables.
# Arguments are passed as block parameters.
@ernie
ernie / call_tracker.rb
Created September 23, 2012 19:57
Injection.rb
#!/usr/bin/env ruby
require './injection'
require 'pry'
class CallTracker < BasicObject
attr_reader :tracked_calls
def initialize
@tracked_calls = ::Hash.new { |h, k| h[k] = 0 }
@ernie
ernie / dance.rb
Created September 23, 2012 19:59
Dependency Injection
#!/usr/bin/env ruby
require './dancer'
require './gangnam_style'
dancer = Dancer.new('PSY', GangnamStyle.new)
dancer.dance!
@ernie
ernie / dot_context.rb
Created May 14, 2013 18:22
Multistache! A multipass rendering example for Mustache.
class DotContext < String
def initialize(val = '.')
val = '.' # Ensure we're always a dot
super
freeze
end
def to_s
self
@ernie
ernie / reorder.rb
Created September 6, 2013 19:20
So, I was in the process of writing this code today, and went on a wild goose chase due to this weird error message. I'd expected to see the nil come back from String#index, and give a different error, but no NilClass to be found in this error. String isn't even a type that String#index can return. It should only return a Fixnum or nil. Instead:…
#!/usr/bin/env ruby
require 'minitest/autorun'
class Reorder
SINGLE_CHAR = /./
def initialize(word, order)
@word, @order = word, order
end
@ernie
ernie / delegate.rb
Last active May 31, 2016 10:39
An alternate take on the delegation class macro provided by ActiveSupport. Updated with Ruby 2.0's caller_locations.
#!/usr/bin/env ruby
class Module
private
def delegate(*args)
dest, prefix = _extract_valid_delegation_options(args.pop)
_define_delegators(caller_locations.first, prefix, dest, args)
end