Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jgn's full-sized avatar
☂️
Messing around.

John Norman jgn

☂️
Messing around.
View GitHub Profile
def assert_equality(example, actual, expected)
print "example #{example}: "
if actual == expected
puts 'Pass'
else
puts "Fail: got #{actual.inspect}; expected #{expected.inspect}"
end
end
examples = [
class HalfAdder
attr_reader :sum, :carry
def initialize(augend, addend)
@augend, @addend = augend, addend
end
def compute
end
end
example [0, 0, 0, 0]: Fail: got nil; expected 0
example [0, 0, 0, 0]: Fail: got nil; expected 0
example [1, 0, 0, 1]: Fail: got nil; expected 0
example [1, 0, 0, 1]: Fail: got nil; expected 1
example [0, 1, 0, 1]: Fail: got nil; expected 0
example [0, 1, 0, 1]: Fail: got nil; expected 1
example [1, 1, 1, 0]: Fail: got nil; expected 1
example [1, 1, 1, 0]: Fail: got nil; expected 0
class HalfAdder
attr_reader :sum, :carry
def initialize(augend, addend)
@augend, @addend = augend, addend
end
def compute
@carry = @augend & @addend
@sum = @augend ^ @addend
class HalfAdder
attr_reader :sum, :carry
def initialize(augend, addend, logger)
@augend, @addend, @logger = augend, addend, logger
end
def compute
@carry = @augend & @addend
@sum = @augend ^ @addend
class HalfAdder
attr_reader :sum, :carry
def initialize(augend, addend, logger)
@augend, @addend, @logger = augend, addend, logger
end
def compute
@carry = @augend & @addend
@sum = @augend ^ @addend
class Logger
attr_reader :log_output
def info(s)
@log_output ||= ""
@log_output << s << "\n"
end
end
examples.each do |example|
a, b, c, s = example
require 'logger'
def get_logger
logger = Logger.new(STDOUT)
def logger.info(s)
@log_output ||= ""
@log_output << s << "\n"
end
def logger.log_output
@log_output
end
class UnexpectedOutputError < StandardError; end
class MockLogger
def initialize(expected_indirect_outputs)
@eio = Array(expected_indirect_outputs)
end
def info(s)
expected = @eio.shift
if expected != s
raise UnexpectedOutputError.new("Fail: got #{s}; expected #{expected}")
end
@jgn
jgn / radix50.rb
Created November 20, 2010 02:14
rad50
#!/usr/bin/env ruby
# http://en.wikipedia.org/wiki/RADIX-50
class String
R50_CHARS = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789"
R50_LOOKUP = {}.tap { |h| String::R50_CHARS.split(//).each_with_index { |c, i| h[c] = i } }
R50_CLEAN = /[[:upper:]|[:digit:]| |.|$|%]/
def radix50
[].tap do |r|
self.upcase.scan(R50_CLEAN).inject([]) do |m, c|
m << R50_LOOKUP[c]