Skip to content

Instantly share code, notes, and snippets.

module Kata
module Base
describe "DSL" do
before :each do
@summary = 'sample summary'
[...]
end
describe "#kata" do
[...]
wesbailey@feynman:~/code_katas> kata take sample.rb
Awesome Exercise Kata
kata "Awesome Exercise" do
[...]
end
@wbailey
wbailey / bundle.sh
Created March 21, 2011 07:51
Announcing the code kata project
wesbailey@feynman:~/tmp/code_katas> bundle install
Using ZenTest (4.4.2)
...
Using bundler (1.0.10)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
@wbailey
wbailey / backreference.rb
Created March 10, 2011 21:08
Using a back reference in the regular expression definition
ruby-1.9.2-p136 > 'asdf asdf--asdf asdf-asdf'.gsub(/( |-)( |-)?/, '_')
=> "asdf_asdf_asdf_asdf_asdf"
ruby-1.9.2-p136 > 'asdf asdf--asdf asdf-asdf'.gsub(/( |-)\1?/, '_')
=> "asdf_asdf_asdf_asdf_asdf"
@wbailey
wbailey / benchmark.rb
Created March 4, 2011 07:31
Fun with ruby arrays and hashes
# usage: array_merge.rb --data_file=filename --limits=10,20,30
require 'rubygems'
require 'vr_script'
require 'benchmark'
include Benchmark
class Array
def to_hash_using_inject_push( use_array )
Hash[*(0 .. self.size).inject( [] ) { |r,i| r.push( self[i], use_array[i] ) }]
@wbailey
wbailey / databases.rake
Created February 11, 2011 08:58
ActiveRecord migrations outside of Rails
require 'yaml'
require 'logger'
require 'active_record'
namespace :db do
def create_database config
options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
create_db = lambda do |config|
ActiveRecord::Base.establish_connection config.merge('database' => nil)
@wbailey
wbailey / 00_create_schema_migrations.down.sql
Created February 8, 2011 10:47
For when you want migrations that are just straight sql files. This is useful when creating databases for working with partner data and matching your data to it another database that is for a rails app.
drop table if exists schema_migrations;
@wbailey
wbailey / tourist_irb_session_1.rb
Created January 26, 2011 22:58
An example irb session to learn how a piece of code that is using meta-programming works
wesbailey@feynman:~/playground> irb -r neverwritecodelikethis
ruby-1.9.2-p0 > space = Hash.new(0)
=> {}
ruby-1.9.2-p0 > ObjectSpace.each_object(self.class) {|o| space[o.class] += 1}
=> 10349
ruby-1.9.2-p0 > space.keys
=> [String, RubyToken::TkIDENTIFIER,..., NeverWriteCodeLikeThis, ... , RubyToken::TkNL, RubyToken::TkRBRACE, ArgumentError]
ruby-1.9.2-p0 >
@wbailey
wbailey / meta_comments.rb
Created January 19, 2011 07:59
Comment your meta-programming to reflect the actual code being executed
class NeverWriteCodeLikeThis
@@actions = ["create", "retrieve", "update"]
def initialize(val)
@@actions.each do |m| # ["create", "retrieve", "update"].each do |m|
instance_variable_set("@#{m}_process".to_sym, val) # instance_variable_set("@create_process".to_sym, val)
end # end
end
end