Skip to content

Instantly share code, notes, and snippets.

View ernie's full-sized avatar

Ernie Miller ernie

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / fun_with_lambdas.rb
Created August 7, 2011 12:00
Fun with lambdas in 1.9
rs = (0..10000).to_a.sample(30)
rs.each do |r|
case r
when :zero?.to_proc then puts "#{r} is zero"
when :even?.to_proc then puts "#{r} is even"
when :odd?.to_proc then puts "#{r} is odd"
else
raise 'unpossible'
end
@ernie
ernie / any.rb
Created July 18, 2011 18:59
Pure-ruby any (quick hack to test something)
module Enumerable
def any?(&block)
self.each do |v|
return true if yield(v)
end
false
end
end
@ernie
ernie / benchmark.rb
Created July 16, 2011 20:46
A benchmark of some enumerable methods
require 'benchmark'
haystack = (1..1_000_000).to_a
needles = 1.upto(100).map {|n| n * 10_000}
module EachDetector
def self.find(haystack, needle)
haystack.each do |v|
return true if v == needle
end