Skip to content

Instantly share code, notes, and snippets.

View ianterrell's full-sized avatar

Ian Terrell ianterrell

  • University of Virginia
  • Charlottesville, VA
View GitHub Profile
module Foo
def self.included(base)
class << base
attr_accessor :bar
end
end
def set
self.class.bar = "hello there"
end
@ianterrell
ianterrell / referral_codes.rb
Created April 18, 2011 14:35
Quickly generate pretty referral codes through Base-N encoding
# Want to give your users nice referral URLs, like http://www.service.com/?r=2ab3 ?
#
# A fast and easy 1-1 method is to use base-n encoded numbers.
#
# For instance, encode the user's id in base 36. In Ruby, this is easy:
# > 272927.to_s(36)
# => "5ulb"
#
# To get the base 10 id back again is just as easy:
# > "5ulb".to_i(36)
@ianterrell
ianterrell / bulk_domain_generator.rb
Created April 16, 2011 16:40
Generate .coms for a bulk search
# Usage:
# ruby bulk_domain_generator.rb word [word...]
suffixes = ['able','ac','acity','ade','age','aholic','al','algia','an','ance','ant','ar','ard','arian','arium','ary','ate','ation','ative','cide','cracy','crat','cule','cy','cycle','dom','dox','ectomy','ed','ee','eer','emia','en','ence','ency','ent','er','ern','escence','ese','esque','ess','est','etic','ette','ful','fy','hood','ial','ian','iasis','iatric','ible','ile','ily','ine','ing','ion','ious','ish','ism','ist','ite','itis','ity','ive','ization','ize','less','let','like','ling','log','ly','ment','ness','oid','ology','oma','onym','opia','opsy','or','ory','osis','ous','path','pathy','phile','phobia','phone','phyte','plegia','plegic','pnea','sect','ship','sion','some','th','tion','trophy','tude','ty','ular','uous','ure','ward','ware','wise','y']
i = 0
ARGV.each do |a|
suffixes.each do |b|
i += 1
puts "#{a}#{b}.com"
@ianterrell
ianterrell / gitdb
Created March 3, 2011 15:09
Small script to
#!/usr/bin/env ruby
# Usage:
# gitdb copy
# Copies the current development database into db/branchname-development.sqlite3
# gitdb
# Copies the db/branchname-development.sqlite3 into the default db/development.sqlite3
branch = `git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'`.gsub('* ','').strip
@ianterrell
ianterrell / Devise BCrypt speed up!
Created January 21, 2011 00:46
Generating users dynamically in your test suite? Speed it up with a simple tweak.
# In your devise.rb configuration file, set the bcrypt stretches to 1 in your test environment:
config.stretches = Rails.env.test? ? 1 : 10
@ianterrell
ianterrell / redis-rb safety patch.rb
Created January 6, 2011 20:09
Monkey patch for safety using flushall and flushdb
require 'redis'
class Redis
alias :__flushall :flushall
alias :__flushdb :flushdb
def flushall(confirm=false)
if confirm
__flushall
else
@ianterrell
ianterrell / Installing Postgres Gems on OS X
Created December 16, 2010 00:11
Installing Postgres Gems on OS X
PATH=$PATH:/Library/PostgreSQL/9.0/bin/ env ARCHFLAGS="-arch x86_64" gem install pg
Loading development environment (Rails 2.3.2)
>> class Z < User; end
=> nil
>> User.subclasses
NoMethodError: protected method `subclasses' called for #<Class:0x22ce974>
from (irb):2
>> User.send(:subclasses)
=> [Z(id: integer, email: string, crypted_password: string, salt: string, created_at: datetime, updated_at: datetime, remember_token: string, remember_token_expires_at: datetime, activation_code: string, activated_at: datetime, state: string, deleted_at: datetime, editor: boolean, has_promise: boolean, reset_code: string)]
>>
[14:04][ian@ian-terrells-macbook-pro:~]$ ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
[14:04][ian@ian-terrells-macbook-pro:~]$ irb
>> class X
>> end
=> nil
>> class Y < X
>> end
=> nil
>> X.subclasses
class Fairish
def initialize(probability, unfair_low, unfair_high, min_rolls)
@probability, @unfair_low, @unfair_high, @min_rolls = probability, unfair_low, unfair_high, min_rolls
@rolls, @hits = 0, 0
end
def fire!
hit = if @rolls >= @min_rolls && observed_probability > @unfair_high
false
elsif @rolls >= @min_rolls && observed_probability < @unfair_low