Skip to content

Instantly share code, notes, and snippets.

@alexch
alexch / benchmarking_require.rb
Created January 4, 2023 18:12
quick and dirty monkey patch to see which `require`d files are taking a long time to load
# quick and dirty monkey patch to see which `require`d files are taking a long time to load
# Usage: 'require "benchmarking_require"' in spec_helper.rb
require "benchmark"
module Kernel
def require_with_benchmark path
x = nil
m = Benchmark.measure do
x = require_without_benchmark path
end
# for use by collection_select and friends, to link a human-readable label with a db-friendly symbolic value
# todo: ActiveRecord macros for setters (for allowing multiple values or just one)
# Usage:
# Table name: snacks
# id :integer
# ice_cream :string
# class Snack < ActiveRecord::Base
# FLAVORS = Enum.new [
# [:vanilla, "Vanilla"],
# [:chocolate, "Chocolate"],
class Animal < ActiveRecord::Base
end
@alexch
alexch / fusion_test.sh
Last active March 14, 2020 01:33
Ghetto Fusion Drive on Old Macs OS X
#!/bin/bash
# Based on http://jollyjinx.tumblr.com/post/34638496292/fusion-drive-on-older-macs-yes-since-apple-has
# To use this, first create a corestorage drive - see e.g. http://www.cnet.com/how-to/how-to-make-a-custom-corestorage-drive-in-os-x/
# -- it'll look something like this:
#
# diskutil list # and figure out which actual drives you want to join
# diskutil cs create FusionGroup disk0 disk2 # or whichever two drives
# diskutil cs list # and note the Logical Volume Group ID
# diskutil cs createVolume GROUPID jhfs+ Fused 100%
# encoding: utf-8
module Kernel
alias :λ :lambda
end
class Hash
alias :+ :merge
alias :<< :merge!
end
@alexch
alexch / Sound.mm
Created April 24, 2013 17:21
AVAudioPlayer makes a loud POP if you tell it to play when it's already playing. So let's make an instance pool and let the other one play out and grab one that's not currently playing. Note that the two sounds will overlap.
-(id)initWithFile: (NSString*)file
{
if (self = [super init]) {
self.file = file;
NSString* path = [[NSBundle mainBundle] pathForResource:self.file ofType:@"caf"];
self.fileUrl = [NSURL fileURLWithPath:path];
NSAssert(self.fileUrl, @"URL is valid.");
self.players = [NSMutableArray arrayWithCapacity:2];
@alexch
alexch / log.txt
Created September 18, 2012 21:04
ActiveRecord create should fail when passed a non-integer for an integer field
demonstration of https://github.com/rails/rails/issues/7690 in a vanilla rails 3.2.8 app
TL;DR: Foo.create(user_id: bob) sets foo's user id to 1, no matter bob's actual id
sesame:rails_bug [ruby-1.9.3-p194] alex$ cat app/models/user.rb
class User < ActiveRecord::Base
attr_accessible :name
has_many :articles
end
@alexch
alexch / afgan.rb
Created September 3, 2012 19:46
script to display afgan tetris GP demo
#!/usr/bin/env ruby
def run delay
chars = File.read("./afgan.demo").split(//)
chars.each do |ch|
$stdout.print ch
$stdout.flush
sleep delay
end
end
@alexch
alexch / application_helper_spec.rb
Created May 10, 2012 21:23
New test for chapter 5 of RailsTutorial
describe ApplicationHelper do
describe "full_title" do
it "includes the page name" do
full_title("foo").should include("foo")
end
it "includes the base name" do
full_title("foo").should include("Ruby on Rails Tutorial Sample App")
end
@alexch
alexch / application.html.erb
Created May 10, 2012 18:00
important parts of chapter 4 of railstutorial
# Listing 4.3. The sample application site layout.
# app/views/layouts/application.html.erb
<title><%= full_title(yield(:title)) %></title>