Skip to content

Instantly share code, notes, and snippets.

@ashmoran
ashmoran / sicp_exercise_2_1.scm
Created January 8, 2011 23:53
SICP exercise 2.1
(define (expect actual expectation expected)
(display "• ")
(display actual)
(display " should equal ")
(display expected)
(display ": ")
(expectation actual expected)
(display "pass\n")
)
@ashmoran
ashmoran / CSharpTestFramework.cs
Created January 17, 2011 23:06
Highly WIP, I'm test-driving a test framework in a language I don't know :-)
using System;
namespace CSharpTestFramework
{
class MainClass
{
private uint run;
private uint failures;
delegate void Test();
@ashmoran
ashmoran / sicp_exercise_2_23.scm
Created January 21, 2011 17:04
SICP Exercise 2.23
(define (my-for-each proc values)
(if (null? values)
#t
(let ()
(proc (car values))
(my-for-each proc (cdr values)))))
(my-for-each (lambda (x) (display x) (newline)) (list 1 3 999 3.14))
(define (my-for-each proc values)
(if (null? values)
#t
(let ()
(proc (car values))
(my-for-each proc (cdr values)))))
(import srfi-1)
(use srfi-1)
@ashmoran
ashmoran / overridden_class_method.rb
Created March 10, 2011 22:11
Example of overriding a method on a Class that is looked up dynamically from an instance
class Foo
class << self
def bar
"Foo.bar"
end
end
def bar
self.class.bar
end
@ashmoran
ashmoran / functional_object_spec.rb
Created March 10, 2011 22:50
A completely stupid example of how to implement classes/objects in Ruby with Procs and Proc#method_missing
class Proc
def method_missing(name, *args)
self[name, *args]
end
end
MyCrazyClass = ->(message, *args) {
case message
when :new
->(message, *args) {
@ashmoran
ashmoran / rake_strip_whitespace.rb
Created March 20, 2011 22:31
Simple Rake task to use sed to strip trailing whitespace from Ruby source
desc "Remove trailing whitespace for source files"
task :strip_whitespace do
files = %w[ .autotest .rspec .rvmrc Gemfile ]
globs = %w[ lib/**/*.rb spec/**/*.rb ]
files_from_globs = globs.map { |glob| Dir[glob] }
files_to_strip = (files + files_from_globs).flatten
system "sed -e 's/[ \t]*$//' -i '' #{files_to_strip.join(" ")}"
end
@ashmoran
ashmoran / class_method_missing.rb
Created March 26, 2011 22:29
Example of using method_missing in a Class
require 'rspec'
class Cow
class << self
def method_missing(message)
new(message.to_s)
end
end
attr_reader :name
@ashmoran
ashmoran / writing_a_file.rb
Created April 3, 2011 15:44
FAO @Ben_Nuttall :)
messages = [ "one,two,three", "un,deux,trois", "eins,zwei,drei" ]
File.open("messages.csv", "w") do |file|
messages.each do |message|
file.puts message
end
end
@ashmoran
ashmoran / database_emptier.rb
Created April 28, 2011 16:17
DatabaseEmptier - generate DELETE statements for a MySQL database with FKs using Sequel and RGL
require 'rubygems'
require 'sequel'
require 'rgl/adjacency'
require 'rgl/topsort'
require 'rgl/transitiv_closure'
require 'rgl/connected_components'
# Usage example below
class DatabaseEmptier