Skip to content

Instantly share code, notes, and snippets.

View mathie's full-sized avatar

Graeme Mathieson mathie

View GitHub Profile
# My new git commit shortcut, thanks to http://whatthecommit.com/ :-)
alias commit='git commit -m "$(curl -s http://whatthecommit.com/ |grep '<p>' |cut -c 4-)"'
#!/usr/bin/env ruby
# A couple of Searchlogic searches. Both are identical from a code
# perspective. With one, the user supplies a '.' (period) as part of the
# search query. This search blows up because it's generating an invalid SQL
# statement. I say it shouldn't. The error in question is:
#
# Mysql::Error: Not unique table/alias: 'users':
#
# SELECT `companies`.`id` AS t0_r0,
# rubygems DNS is temporarily down, if you're using ghost to manage local DNS
# munging, run the following:
ghost add rubygems.org 72.4.120.124
ghost add production.s3.rubygems.org 207.171.181.231
ghost add production.cf.rubygems.org 216.137.45.24
# Don't forget to clear them out afterwards with:
# ghost delete_matching rubygems.org
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require "#{RAILS_ROOT}/config/environment"
crud_actions = [ "index", "show", "new", "edit", "create", "update", "destroy" ]
# Force all the controllers in the application to be loaded.
Dir['app/controllers/**/*_controller.rb'].each { |f| load f }
all_controllers = ObjectSpace.each_object.select do |object|
object.class == Class && object.name =~ /Controller$/

Hash ordering test

This test passes on my system right now. (It might not on your system, but that's not important. Change the assert_equal so that it does.)

Now uncomment the @other_hash declaration. It (probably) fails.

That makes the hashing algorithm in Ruby dependent upon the code that runs before it. Which, when you accidentally depend upon the order of keys in a hash, makes for a lot of confusion later on when it no longer works!

The particular example involved verifying that a model accepted nested attributes. We were doing something along the lines of:

This was a quick hack attempt to create a simple factory_girl-like thing for objects that weren't ActiveRecord-based at all, just plain ol' objects with attr_accessors.

Didn't work quickly enough to pursue, but I didn't want to throw it away either. :-)

function smiley() {
if [ $? = 0 ]; then
printf $1
else
printf $2
fi
return $?
}
# Show lots of info in the __git_ps1 output.
Graeme has been making a nuisance of himself on the Internet and in various
open source communities since 1992 when he managed to blag a 9k6 modem from
school. He didn't discover Ruby until late 2005 when he was frustrated with
Zope (Python's only web framework at the time) and heard about this thing
called Rails.
Graeme was so impressed with Rails (and Ruby) that he quit his day job and
started his own company, Rubaidh, so he could work with it full time. Since
then he's worked with a variety of companies – from tiny startups to
household names – using Ruby to deliver reliable, usable, secure software.
begin
require 'readline'
class PersistentHistory
attr_accessor :number_of_lines_of_history_to_save, :history_file
def initialize(options = {})
self.number_of_lines_of_history_to_save = options[:number_of_lines_of_history_to_save]
self.history_file = options[:history_file]
task :prevent_brainfart_apocalypse do
raise "You're running in production" if Rails.env.production?
end
protect_me_from_myself_tasks = %W(db:setup)
protect_me_from_myself_tasks.each do |task_name|
Rake::Task[task_name].prerequisites.insert 0, 'prevent_brainfart_apocalypse'
end