Skip to content

Instantly share code, notes, and snippets.

View blaix's full-sized avatar

Justin Blake blaix

View GitHub Profile
class EvolutionMachine
def initialize(max_children = 100, max_mutation = 3)
@max_children = max_children
@max_mutation = max_mutation
end
def evolve(phrase, target)
normalize(phrase, target)
puts phrase
while(phrase != target)
@blaix
blaix / gist:350713
Created March 31, 2010 18:50
An example of why blocks are awesome.
class SomeService
def initialize(host)
@connection = SomeConnectionClass.new(host)
end
# class-level method
def self.open(host)
instance = self.new(host)
yield(instance)
ensure
@blaix
blaix / gist:350714
Created March 31, 2010 18:51
Showing off some niceties of ruby.
class Person
attr_reader :name
def initialize(name, height)
@name = name
@height = height
end
def tall?
@height == "tall"
@blaix
blaix / gist:351961
Created April 1, 2010 15:42
Another block example: allows creation of dsl-like diddys like this (also makes use of some neat ruby metapgramming).
require 'builder'
xml = Builder::XmlMarkup.new
xml.instruct!
xml.foo do
xml.bar do
xml.baz("suckas")
end
xml.no_closing_tag
@blaix
blaix / 1-normal.rb
Created April 5, 2010 17:06
What is the best way to solve the dependency injection "problem"?
class Foo
def initialize
@logger = Logger.new("logfile.log")
end
def do_something
# do something...
@logger.log("doing something")
end
end
# Install ruby 1.8.6 using the one-click windows installer from:
# http://www.ruby-lang.org/en/downloads/
# Be sure to check the box to enable rubygems during the install.
# Make sure C:\bin exists and is in your path.
# Now open a command-prompt and run:
> gem install pik
> pik_install C:\bin
> pik install ruby 1.8.7
> pik use 187
Given /^I see the page$/ do
puts @response.body
end
Given /^I see the url$/ do
puts @request.url
end
Given /^I see the current user$/ do
puts @controller.send(:current_user).inspect
@blaix
blaix / factory_girl_steps.rb
Created July 22, 2010 13:37
A modified version of the cucumber steps included in factory_girl. But I'd like to find a way to simply redefine the steps it creates without raising an ambiguos step error...
# Stolen from: http://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/step_definitions.rb
# and slightly modified so that steps which create only a single record, save
# that record to an instance variable, so you can do things like:
#
# Given I am logged in as an admin
# And a post exists with a title of "Awesome Post"
# When I go to the post
# Then I should see "Awesome Post"
# And I should see a link to edit the post
#
@blaix
blaix / snow_leopard_terminal_colors.markdown
Created July 23, 2010 14:30
Pretty Terminal colors in OS X 10.6 Snow Leopard

For pretty, readable, light-on-dark colors in Terminal.app:

After reading a handful of blog posts I finally got this working in 10.6...

  1. Add this to ~/.bash_profile:

     export CLICOLOR=1
    
  2. Install SIMBL.

@blaix
blaix / iterm_pastel_theme.sh
Created July 23, 2010 17:14
iTerm light-on-dark profile.
#!/bin/bash
# Pretty light-on-dark theme for iTerm.
# Copied from here:
# http://kpumuk.info/mac-os-x/customizing-iterm-creating-a-display-profile-with-pastel-colors/
# Run in terminal then restart iTerm.
# Profile is listed as "Pastel" under Bookmarks => Manage Profiles...
PASTEL='{