Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / article.rb
Created May 16, 2012 15:12
Manual cache invalidation
class Article < ActiveRecord::Base
def publish
self.published_at = Time.current
saved = save
Rails.cache.clear("published_articles")
saved
end
end
@jgaskins
jgaskins / gist:2784127
Created May 24, 2012 20:44
Blocks are, indeed, represented as Proc objects
def m &block
block
end
def n &block
block.call
end
p n(&m { 'This is my block' })
# => "This is my block"
@jgaskins
jgaskins / fake-gem.gemspec
Created June 2, 2012 21:20
Let's pretend this is a gem
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "not-really-a-gem"
s.version = 0.0.0
s.authors = ["Jamie Gaskins"]
s.homepage = "https://gist.github.com/2859988"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@jgaskins
jgaskins / commits.rb
Created June 22, 2012 06:03
See the verbs used in your commit messages
# Usage: ruby commits.rb <user> <repo>
require 'open-uri'
require 'json'
user = ARGV[0]
repo = ARGV[1]
commits = JSON.parse(open("https://github.com/#{user}/#{repo}/commits.json").read)["commits"]
messages = commits.map{ |c| c["message"] }
@jgaskins
jgaskins / foo.rb
Created June 25, 2012 08:56
Private attr_{reader,writer,accessor} methods
class Foo
private
attr_reader :bar
end
p Foo.new.bar # => Chucks an exception for private method 'bar'
@jgaskins
jgaskins / 0-README.md
Created July 24, 2012 11:37
Compare performance of polymorphic vs if-guarded method dispatch

What is this?

I decided to compare the performance of using if-based execution against actual polymorphic method calls on the big 3 Ruby implementations. The results on MRI were what I expected across all of them — that is, polymorphism being much faster. The results are across 1M objects in an array.

In the case of MRI 1.9, dynamic dispatch is 58% faster than explicit if-based branching. On JRuby, the runtimes are nearly identical. And on Rubinius, polymorphic calls are slightly faster (about 25%).

MRI 1.9

                  user     system      total        real
@jgaskins
jgaskins / application_helper_spec.rb
Created July 26, 2012 18:41
Testing Rails helpers
require 'ostruct'
require File.expand_path('../../../app/helpers/application_helper', __FILE__)
describe ApplicationHelper do
let(:current_user) { OpenStruct.new(email: 'me@example.com', name: 'Foo Bar') }
let(:view) { double('view') }
subject { view }
before do
view.extend ApplicationHelper
@jgaskins
jgaskins / results.md
Created October 15, 2012 13:25
Custom test doubles vs OpenStruct vs RSpec doubles

Instantiating: creates I test doubles with N methods each. Calling methods: calls each method on the test double I times.

  • 5 methods per test double, 1000 iterations:
    • 4720ms: Instantiating RSpec doubles
    • 163.91ms: Calling methods on RSpec doubles
    • 126.35ms: Instantiating OpenStructs
    • 65.96ms: Instantiating MyDouble class
    • 18.74ms: Calling methods on OpenStructs
  • 17.2ms: Calling methods on MyDoubles
@jgaskins
jgaskins / mapper.rb
Created November 15, 2012 13:54
Perpetuity::Mapper idea
require 'set'
class Mapper
def initialize
self.class.base_class.add_mapper self.class
end
def self.mappers
@mappers ||= Set.new
end
@jgaskins
jgaskins / gist:4739788
Created February 8, 2013 15:41
Clojure's `iterate` function in Ruby
class Object
def iterate(&block)
Enumerator.new do |yielder|
current = self
loop do
yielder << current
current = yield(current)
end
end
end