Skip to content

Instantly share code, notes, and snippets.

View olivierlacan's full-sized avatar

Olivier Lacan olivierlacan

View GitHub Profile
diff --git i/hash.c w/hash.c
index 007508a..6f39e47 100644
--- i/hash.c
+++ w/hash.c
@@ -2402,6 +2402,28 @@ rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
return ary;
}
+static int
+hash_comprised_i(VALUE key, VALUE value, VALUE arg)
@olivierlacan
olivierlacan / database.rake
Last active June 10, 2021 07:06
Database rake tasks that I use on Code School to ferret out huge tables with millions of rows and see how many indices they have and to see which tables have missing indices on associated tables (foreign keys). The latter was taken from this great post by Tom Ward: https://tomafro.net/2009/09/quickly-list-missing-foreign-key-indexes
namespace :database do
task fat_tables: :environment do
c = ActiveRecord::Base.connection
max_table_name_width = 0
tables = c.tables.sort_by do |t|
max_table_name_width = t.length if t.length > max_table_name_width
@olivierlacan
olivierlacan / add_index_on_client_application_id_for_oauth_tokens.rb
Created October 17, 2014 07:17
Example of a (slightly hacky) concurrent migration for Rails 3.2 to add an index to a huge table without locking the table in a transaction.
class AddIndexOnClientApplicationIdForOauthTokens < ActiveRecord::Migration
def ddl_transaction(&block)
# hack because AR 3.x doesn't support the `disable_ddl_transaction!`
# method that AR 4.x introduced yet.
block.call # do not start a transaction
end
def change
# using raw SQL because ActiveRecord 3.x doesn't support concurrent
# migrations yet, in AR 4 this would be:
@olivierlacan
olivierlacan / output.bash
Created September 29, 2014 15:40
Does it really make sense to optimize for a 1.01x speed improvement by using single-quoted strings instead of double-quoted Strings in Ruby (to avoid interpolation scanning in the double-quoted strings)? I think the answer is no.
$ ruby string_benchmark.rb
Calculating -------------------------------------
single quote 128800 i/100ms
double quote 128351 i/100ms
-------------------------------------------------
single quote 6144842.6 (±5.4%) i/s - 30654400 in 5.008373s
double quote 6089768.4 (±6.4%) i/s - 30290836 in 5.000430s
Comparison:
single quote: 6144842.6 i/s
@olivierlacan
olivierlacan / merge_var_content_checker.rb
Created June 8, 2014 14:48
RSpec matcher for mandrill_mailer gem that allows you to check the main CONTENT merge var for a specific string using expect(mailer).to include_merge_var_content('my string')
# Public: Matcher for asserting specific merge vars content contains something.
#
# merge_var_key - Key of the merge var whose content will be checked
# expected_data - Data to look for in the specified merge var key.
#
# WelcomeMailer is an instance of MandrillMailler::TemplateMailer
#
# let(:user) { FactoryGirl.create(:user) }
# let(:mailer) { WelcomeMailer.welcome_registered(user) }
# it 'should have the correct data' do
@olivierlacan
olivierlacan / Hash#contain?.patch
Last active August 29, 2015 14:00 — forked from nobu/Hash#comprized?.diff
Proposed implementation for Hash#contains? created by Nobu and slightly tweaked for semantics. Details here: http://olivierlacan.com/posts/proposal-for-a-better-ruby-hash-include/
diff --git i/hash.c w/hash.c
index 007508a..6f39e47 100644
--- i/hash.c
+++ w/hash.c
@@ -2402,6 +2402,28 @@ rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
return ary;
}
+static int
+hash_contain_i(VALUE key, VALUE value, VALUE arg)
@olivierlacan
olivierlacan / keybase_proof.md
Created April 17, 2014 10:02
Keybase.io Proof

Keybase proof

I hereby claim:

  • I am olivierlacan on github.
  • I am olivierlacan (https://keybase.io/olivierlacan) on keybase.
  • I have a public key whose fingerprint is 657E 4315 B9D1 4DA4 EC57 152C 8AF8 9C6A 1FF9 AF6D

To claim this, I am signing this object:

@olivierlacan
olivierlacan / 1_nickel.rb
Last active August 29, 2015 13:57
Trying to figure out why I'm having to manually require a class that is within the same namespace inside of Rails.
# located in lib/pumper/nickel.rb
# the only way Back.setup works inside of #add_crappy_music
# is if I add the following:
# require "pumper/nickel/back"
#
# Why?
module Pumper
class Nickel < ActiveRecord::Base
def add_crappy_music
@olivierlacan
olivierlacan / gist:8162452
Created December 28, 2013 18:24
RSpec error under Ruby 2.1.0 and Rails 4.1.0.beta.1
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
From:
/Users/olivierlacan/rails41app/.bundle/bundler/gems/rails-8003c541f7e6/activesupport/lib/active_support/dependencies.rb:247:in `require'
/Users/olivierlacan/rails41app/.bundle/bundler/gems/rails-8003c541f7e6/activesupport/lib/active_support/dependencies.rb:247:in `block in require'
/Users/olivierlacan/rails41app/.bundle/bundler/gems/rails-8003c541f7e6/activesupport/lib/active_support/dependencies.rb:232:in `load_dependency'
/Users/olivierlacan/rails41app/.bundle/bundler/gems/rails-8003c541f7e6/activesupport/lib/active_support/dependencies.rb:247:in `require'
/Users/olivierlacan/rails41app/.bundle/gems/shoulda-matchers-2.4.0/lib/shoulda/matchers/assertion_error.rb:10:in `<module:Matchers>'
/Users/olivierlacan/rails41app/.bundle/gems/shoulda-matchers-2.4.0/lib/shoulda/matchers/assertion_error.rb:2:in `<module:Shoulda>'
/Users/olivierlacan/rails41app/.bundle/gems/should