Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / 0-results
Created February 23, 2014 04:39
Benchmarking Nokogiri, LibXML, and Ox
Parsing...
Calculating -------------------------------------
nokogiri 127 i/100ms
libxml 130 i/100ms
ox 716 i/100ms
-------------------------------------------------
nokogiri 1334.7 (±15.1%) i/s - 6604 in 5.215835s
libxml 1287.5 (±1.9%) i/s - 6500 in 5.050367s
ox 7228.3 (±3.7%) i/s - 36516 in 5.059256s
@jgaskins
jgaskins / application.rb
Last active August 29, 2015 13:57
Charging customers during registration with Devise
module Foo
class Application < Rails::Application
# Keep the credit card data out of your logs
config.filter_parameters += [:password, :credit_card]
end
end
@jgaskins
jgaskins / ar_model.rb
Created March 28, 2014 01:19
ActiveRecord default attributes
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection host: 'localhost',
database: 'jamie',
adapter: 'postgresql'
class ARModel < ActiveRecord::Base
def initialize(attributes={})
# AR::Base#create w/o args passes nil because lol.
@jgaskins
jgaskins / Gemfile
Created April 23, 2014 13:27
Backporting Rails 4 caching to Rails 3.2
# Add these two gems
gem 'thread_safe'
gem 'cache_digests'
@jgaskins
jgaskins / fib.rb
Created June 5, 2014 00:56
Fibonacci Benchmark: Swift vs Rubinius vs JRuby vs MRI
require 'benchmark'
def fib n
if n <= 1
n
else
fib(n - 1) + fib(n - 2)
end
end
@jgaskins
jgaskins / foo.rb
Created June 13, 2014 20:15
Short-circuiting OR/AND in Ruby, represented as methods
# If it were possible to override && and || in Ruby, this is how it might look.
class NilClass
def && other
self
end
def || other
other
end
@jgaskins
jgaskins / foo.coffee
Last active August 29, 2015 14:04
Default values
class Foo
def initialize: (attributes={}) ->
# Set default day to Monday
# This broke for Sunday (attributes.day == 0)
@day = attributes.day || 1
# How I ended up doing it
@day = if attributes.hasOwnProperty('day') then attributes.day else 1
@jgaskins
jgaskins / articles_controller.rb
Created August 19, 2014 03:37
Controllers with AR vs Perpetuity
class ArticlesController < ApplicationController
# with ActiveRecord
def create
@article = Article.new(params[:article])
if @article.save
redirect_to @article, notice: 'Article created!'
else
render :new
end
#!/usr/bin/env ruby
puts ARGF.read
@jgaskins
jgaskins / customer_accounts.rb
Last active August 29, 2015 14:12
Example displaying eager loading with perpetuity-postgres for https://github.com/jgaskins/perpetuity-postgres/issues/18
require 'perpetuity/postgres'
require 'securerandom'
require 'pp'
Perpetuity.data_source 'postgres://localhost/customer_stuff'
module Customer
class User
attr_accessor :name, :email, :api_key, :accounts