Skip to content

Instantly share code, notes, and snippets.

@jimweirich
jimweirich / allocation.rb
Last active January 3, 2016 05:59
This is a refactoring of the Allocation Elimination code. The original code (by Mike Doel and Adam McRae) was originally refactored by a large group at the Neo pairing booth at CodeMash. I then took the final version there and continued to refactor over the course of an evening and this is the result of that. I hope to blog about the refactoring…
Allocation = Struct.new(:project, :person, :start, :finish) do
include Comparable
def mergable_with?(other)
if start > other.start
other.mergable_with?(self)
else
same_assignment?(other) &&
finish >= other.start - 1
end
@jimweirich
jimweirich / beer_song_test.rb
Created November 19, 2013 17:07
For Cincy.rb tonight. Download the following test file. Remove the first "skip" command get the test to pass. Then remove the next "skip" command and continue.
gem 'minitest'
require 'minitest/autorun'
require './beer_song'
class BeerSongTest < Minitest::Test
V8 =
"8 bottles of beer on the wall, 8 bottles of beer.\n" +
"Take one down and pass it around, 7 bottles of beer on the wall.\n"
def new
allocate.tap { |obj| obj.initialize }
end
@jimweirich
jimweirich / Rakefile
Last active January 10, 2019 05:12
Bottles of Beer, for Sandi Metz
#!/usr/bin/env ruby
require 'rake/clean'
require 'rake/testtask'
task :default => [:spec, :test]
task :spec do
sh "rspec ."
end
@jimweirich
jimweirich / weird.rb
Created November 5, 2013 03:51
What does the following print? I was surprised. (Actually, I was surprised it was legal syntax.)
def foo(a=:default, b)
puts "a=#{a}, b=#{b}"
end
foo(:value)
foo(:x, :y)
@jimweirich
jimweirich / configure.rb
Created October 2, 2013 01:12
Demonstrating a flexible DSL for configuration .
def project(name, &block)
Project.new(name, &block)
end
class Project
def initialize(name, &block)
@name = name
@context = eval("self", block.binding)
instance_eval(&block) if block_given?
$ rspec xx_spec.rb
....
Finished in 0.00287 seconds
4 examples, 0 failures
@jimweirich
jimweirich / spy_test_fragment.rb
Created August 31, 2013 15:57
Example how to use argument matchers with spies.
def test_spy
d = flexmock(f: :ok)
assert_equal :ok, d.f("Woof",2)
assert_spy_called d, :f, String, Integer
end
@jimweirich
jimweirich / JRuby_Output.txt
Last active December 21, 2015 23:18
Differences between JRuby and Ruby2 Ripper output
SOURCE: defined?(a)
RESULT:
[:program, [:stmts_add, [:stmts_new], [:defined, [:@rparen, ")", [1, 10]]]]]
SOURCE: "my name is #{name}"
RESULT:
[:program,
[:stmts_add,
[:stmts_new],
[:string_literal,
@jimweirich
jimweirich / sqrt_spec.rb
Created August 10, 2013 21:19
Square Root function
# -*- coding: utf-8 -*-
Sqrt = ->(n) {
guess = n / 2.0
loop do
if (guess**2 - n) < 0.0001
return guess
end
guess = (guess + n/guess) / 2.0
end