Skip to content

Instantly share code, notes, and snippets.

View bakineggs's full-sized avatar

Dan Barry bakineggs

View GitHub Profile
arr = (0...20).to_a
p0 = 0
(0..8).each do |x|
p0 += 0.475 ** x * 0.525 ** (20 - x) * arr.combination(x).count
end
p1 = 0.475 ** 9 * 0.525 ** 11 * arr.combination(9).count
p2 = 0
@bakineggs
bakineggs / feet.rb
Created July 25, 2016 01:04
making feet for my dining table
raise "Usage: ruby #{__FILE__} <roughing file> <finishing file>" unless ARGV.length == 2
X_SIZE = 34.0 # leave 2" for mounting to spoil board
Y_SIZE = 3.5
Z_SIZE = 3.5
X_INCR = 0.001
STUB_WIDTH = 1.625
STUB_HEIGHT = 1.5625
describe "#my_method" do
before do
@blog_post = FactoryGirl.create(:blog_post)
end
it "should do something" do
@blog_post.title.should == 'mytitle'
end
end
# This patch enables access to the records being preloaded when eager loading
# an association whose conditions use interpolation to call class methods.
module ActiveRecord::AssociationPreload::ClassMethods
[:has_and_belongs_to_many, :has_one, :has_many, :belongs_to].each do |type|
define_method "preload_#{type}_association_with_access_to_records" do |records, reflection, *args|
@records_to_preload_for_reflection ||= {}
@records_to_preload_for_reflection[reflection] = records
send "preload_#{type}_association_without_access_to_records", records, reflection, *args
@records_to_preload_for_reflection.delete reflection
@bakineggs
bakineggs / app__controllers__tabs_controller.rb
Created November 11, 2012 01:10
Eager load an association that uses interpolation for a key constraint
class TabsController < ApplicationController
def index
@tabs = Tab.all :include => :available_balance
end
end
@bakineggs
bakineggs / database_schema_spec.rb
Created October 3, 2012 07:05
Test to ensure people update db/schema.rb when they add a migration
# spec/lint/database_schema_spec.rb
describe 'db/schema.rb' do
it 'includes the changes from all migrations' do
if checksum_line = File.read('db/schema.rb').split("\n").detect {|line| line.match /^# Checksum: \w+$/}
checksum = checksum_line.match(/^# Checksum: (\w+)$/)[1]
checksum.should == Digest::MD5.hexdigest(Dir.glob('db/migrate/*').sort.map {|path| File.read path}.join "\n")
else
fail 'db/schema.rb does not contain a migration directory checksum! Please run rake db:migrate'
end
@bakineggs
bakineggs / gist:3823667
Created October 2, 2012 22:16
HashWithIndifferentAccess equality
$ bundle exec script/console
Loading development environment (Rails 2.3.14)
>> {:a => :b} == {:a => :b}.with_indifferent_access
=> false
>> {:a => :b} == {'a' => :b}.with_indifferent_access
=> false
>> {'a' => :b} == {:a => :b}.with_indifferent_access
=> true
>> {'a' => :b} == {'a' => :b}.with_indifferent_access
@bakineggs
bakineggs / contains_array.rb
Created June 6, 2012 19:01
matchers for testing that one hash/array is contained in another
# contain_array checks to see if one array contains all of the elements from another array in the same relative order
#
# Any elements that are hashes are matched using contain_hash
# Any elements that are arrays are matched using contain_array
#
# Examples:
#
# describe 'something' do
# subject do
# [:a, {:b => [:c, :d, :e]}]
@bakineggs
bakineggs / smart_shark.rb
Created January 3, 2012 20:05
figures out credit worthiness of borrowers
require 'fastercsv'
require 'mrscake'
require 'open-uri'
def download_loans
open 'https://www.lendingclub.com/info/download.action?file=LoanStats.csv' do |source|
open 'LoanStats.csv', 'w' do |destination|
destination.write source.read
end
end
@bakineggs
bakineggs / .bash_profile
Created October 25, 2011 21:52
prompt tells me about possibly problematic git author setting
# Git completion
if [[ -f "$HOME/.git-completion.bash" ]]; then
source "$HOME/.git-completion.bash"
__has_gitdir=true
elif [[ -n $(which brew) && -f "`brew --prefix`/etc/bash_completion.d/git-completion.bash" ]]; then
. `brew --prefix`/etc/bash_completion.d/git-completion.bash
__has_gitdir=true
else
function __git_ps1() {
printf ""