Skip to content

Instantly share code, notes, and snippets.

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
# [:vanilla, "Vanilla"],
# [:chocolate, "Chocolate"],
View animal.rb
class Animal < ActiveRecord::Base
alexch /
Last active Mar 14, 2020
Ghetto Fusion Drive on Old Macs OS X
# Based on
# To use this, first create a corestorage drive - see e.g.
# -- 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
class Hash
alias :+ :merge
alias :<< :merge!
alexch /
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.
-(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 / log.txt
Created Sep 18, 2012
ActiveRecord create should fail when passed a non-integer for an integer field
View log.txt
demonstration of 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
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 ="./afgan.demo").split(//)
chars.each do |ch|
$stdout.print ch
sleep delay
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")
it "includes the base name" do
full_title("foo").should include("Ruby on Rails Tutorial Sample App")
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")
You can’t perform that action at this time.