Skip to content

Instantly share code, notes, and snippets.

View iain's full-sized avatar

iain iain

View GitHub Profile
# hoptoad webistrano recipe
# works with Rails 3
after "deploy", "deploy:notify_hoptoad"
after "deploy:migrations", "deploy:notify_hoptoad"
namespace :deploy do
desc "Notify Hoptoad of the deployment"
task :notify_hoptoad do
rails_env = fetch(:hoptoad_env, fetch(:rails_env, "production"))
@iain
iain / game_of_life.rb
Created April 30, 2010 00:06
Conway's Game of Life, in one line of Ruby
# Copyright 2010, Iain Hecker. All Rights Reserved
# Conway's Game of Life, in one line of Ruby.
# http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
# Tested and found working on Ruby 1.8.7 and 1.9.2
# The grid is spherical or "wrap around", so the left is connected to the right and top to bottom.
#
# Generate a random grid, 30 cells wide and 10 cells high
#
# grid = "30x10".to_grid
#
@iain
iain / .irbrc.rb
Created April 16, 2010 10:06
My ~/.irbrc file
# IRBRC file by Iain Hecker, http://iain.nl
# put all this in your ~/.irbrc
require 'rubygems'
require 'yaml'
alias q exit
class Object
def local_methods
(methods - Object.instance_methods).sort
@iain
iain / post.rb
Created April 10, 2010 13:50
Simple Rspec example
class Post
def initialize(attributes = {})
@attributes = attributes
end
def publishable?
@attributes[:approved]
end
# Writing Genesis in Ruby, for http://www.whatdigitalrevolution.com/?p=119
God = "God"
def God.performs &acts
instance_eval &acts
end
def God.create *stuff
"#{self} created #{stuff.join(' and ')}."
# Without Symbol#to_proc
[1, 2, 3].map { |it| it.to_s }
[3, 4, 5].inject { |memo, it| memo * it }
# With Symbol#to_proc
[1, 2, 3].map(&:to_s)
[3, 4, 5].inject(&:*)
@iain
iain / jquery-bind.js
Created January 11, 2010 23:02
bind scopes to callbacks in jQuery
var MyClass = new Function();
MyClass.prototype = {
/**
* bind(Function) -> Function
*
* The problem with jQuery is that jQuery methods change +this+ inside their
* callbacks. This looses your reference to the current object. Pass the
* callback-function to +bind+ and it fixes it for you.
*
require File.join(File.dirname(__FILE__), 'config', 'environment')
require 'ya2yaml'
module PrettyYaml
class << self
def write(filename, hash)
File.open(filename, "w") do |f|
f.write(yaml(hash))
end
end
# This a sample YML file from Rails 2.3. The objective is to propose a yml based on this
# one which will reduce error messsages duplication, as outline in this post:
#
# http://groups.google.com/group/rails-i18n/browse_thread/thread/3085a78831ed8fae
#
# Proposals are available below and also check the forks at the right.
en:
activerecord:
models:
admin: "Admin"
@iain
iain / gist:243495
Created November 26, 2009 14:47
Using all as a scope
class ActiveRecord::Base
named_scope :all, lambda { |*options| options.first || {} }
end