Skip to content

Instantly share code, notes, and snippets.

Alex Chaffee alexch

Block or report user

Report or block alexch

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View enum.rb
# 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"],
View animal.rb
class Animal < ActiveRecord::Base
end
@alexch
alexch / fusion_test.sh
Last active Sep 22, 2018
Ghetto Fusion Drive on Old Macs OS X
View fusion_test.sh
#!/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%
View aliases.rb
# encoding: utf-8
module Kernel
alias:lambda
end
class Hash
alias :+ :merge
alias :<< :merge!
end
@alexch
alexch / Sound.mm
Created Apr 24, 2013
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.
View Sound.mm
-(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 Sep 18, 2012
ActiveRecord create should fail when passed a non-integer for an integer field
View log.txt
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 Sep 3, 2012
script to display afgan tetris GP demo
View afgan.rb
#!/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
New test for chapter 5 of RailsTutorial
View application_helper_spec.rb
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
important parts of chapter 4 of railstutorial
View application.html.erb
# Listing 4.3. The sample application site layout.
# app/views/layouts/application.html.erb
<title><%= full_title(yield(:title)) %></title>
View pig_latin.rb
def translate(str)
tr_words = []
words = str.split
words.each do |word|
if word =~ /^([bcdfghjklmnpqrstvwxz]+)(.*)/
p word
p $1
p $2
tr_words.push($2 + $1 + "ay")
else
You can’t perform that action at this time.