Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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
#
# 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"))
Rake.application.instance_variable_get('@tasks')['default'].prerequisites.delete('test')
task :default => :spec
task :stats => "spec:statsetup"
desc "Run all specs in spec directory (excluding plugin specs)"
task :spec => :"spec:load" do
RSpec.run(:all) # or something similar
end
module RSpec
module Expectations
# If Test::Unit is loaed, we'll use its error as baseclass, so that Test::Unit
# will report unmet RSpec expectations as failures rather than errors.
superclass = ['Test::Unit::AssertionFailedError', '::StandardError'].map do |c|
eval(c) rescue nil
end.compact.first
class ExpectationNotMetError < superclass
def spec_failure?
ActiveRecord::StatementInvalid (Mysql2::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation 'like': SELECT `tags`.* FROM `tags` WHERE (name LIKE '☃')):
@iain
iain / validation_matcher.rb
Created October 31, 2010 20:09
Experimenting with a validation matcher for RSpec 2
module ValidationMatcher
class Validate
include RSpec::Matchers::Pretty
attr_accessor :attribute, :values, :target, :results, :value_description
def initialize(attribute)
self.attribute = attribute
end
ruby-1.9.2@fuubar fuubar develop / $ rspec spec
FFFFFFFFFFFFFFFFFFF
Failures:
1) Fuubar start should create a new ProgressBar
Failure/Error: @output = StringIO.new
uninitialized constant RSpec::Core::ExampleGroup::Nested_1::Nested_1::StringIO
# ./spec/fuubar_spec.rb:6:in `block (2 levels) in <top (required)>'
2) Fuubar start should set the title
God.watch do |w|
DEFAULT.call(w)
w.name = "delayed_job"
w.start = "bundle exec #{RAILS_ROOT}/script/delayed_job -e #{RAILS_ENV} run"
w.pid_file = "#{RAILS_ROOT}/tmp/pids/delayed_job.pid"
end