Skip to content

Instantly share code, notes, and snippets.

kerryb / gist:66557
Created Feb 18, 2009
Asserting true and false (ish) in rspec
View gist:66557
# If you want to check that something's true or false in the normal ruby sense
# (ie anything other than false or nil is true), you can use double negation
# to force it into an actual boolean (eg !!foo.should be_true). To keep this
# ugliness out of your tests, stick these matchers in your spec helper:
def be_truish
return simple_matcher do |obj, matcher|
matcher.description = 'be true(ish)'
matcher.failure_message = "Expected #{obj.inspect} to be true(ish)"
matcher.negative_failure_message = "Expected #{obj.inspect} not to be true(ish)"
View rspec_gotchas.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'spec'
class Foo
rescue StandardError
View gist:274297
# A script to run ctags on all .rb files in a project. Can be run on
# the current dir, called from a git callback, or install itself as a
# git post-merge and post-commit callback.
CTAGS = '/usr/local/bin/ctags'
HOOKS = %w{ post-merge post-commit post-checkout }
HOOKS_DIR = '.git/hooks'
View Trailing space rake task
namespace :code do
task :trailing_spaces do
grep "app", "spec"
def grep *file_patterns
files_found = ""
file_patterns.each do |file_pattern|
files_found << `grep -r -E '^.*[[:space:]]+$' --include '*.rb' #{file_pattern}`
kerryb / breadcrumbs.rb
Created Nov 11, 2010
Which is best, iteration or recursion?
View breadcrumbs.rb
# I originally refactored this method to a recursive solution, but I wonder whether
# that's really necessary. Is the alternative approach using inject easier to
# understand?
# Original method, with external iterator:
# Build breadcrumb list from an array of nested model instances, starting with the parent
# and ending with the lowest level child. Each breadcrumb needs to be created using a
# sub-array containing itself and its parents.
View gist:905396
$ rake spec
(in /Users/kerry/tmp/foo)
/Users/kerry/ruby/bin/ruby -S bundle exec rspec ./spec/models/foo_spec.rb
1) Foo shows the line that failed
Failure/Error: false.should be_true
expected false to be true
kerryb /
Created Jul 15, 2011
My solution to Neil's bash masterclass exercise
# Number guessing game.
# A simple demo of bash capabilities
readonly MaxGuesses=6
readonly Timeout=10
kerryb / dynamic_modules.rb
Created Nov 9, 2011
dynamically including a module depending on an instance property
View dynamic_modules.rb
module DynamicSetup
def dynamic_setup_complete?
def method_missing name, *args, &block
if dynamic_setup_complete?
kerryb / chunked_average.rb
Created Mar 9, 2012
Reduce list to average of chunks of n items
View chunked_average.rb
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
chunk_size = 3
chunked_averages = ({|a| a.first / chunk_size}{|a| / a.size}
View gist:2028383
$ gem -v
<internal:lib/rubygems/custom_require>:32:in `rescue in require': undefined method `try_activate' for Gem:Module (NoMethodError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems.rb:32:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from /usr/local/bin/gem:8:in `<main>'
$ ruby -d -rubygems -e 'puts 42'