This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby19 | |
require 'optparse' | |
# Report erros that are caused by invoking a method when | |
# an instance is not in the proper state to do so. | |
class StateError < Exception | |
end | |
# Instances of class Tee can send output to several destinations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Visitor | |
def traverse(enum) | |
enum.each do |node| | |
visit node | |
end | |
self | |
end | |
def visit(node) | |
node.class.ancestors.each do |cl| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
Track = Struct.new :title, :duration | |
# An Album represents an audio medium which has title, | |
# interpret, a pause duration between tracks and a list | |
# of individual tracks. | |
class Album | |
attr_reader :title, :interpret, :pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
# information about a single line | |
LineInfo = Struct.new :file_name, :lineno, :line | |
# comparison data | |
FileData = Struct.new :file_a, :file_b | |
# read all words from first file and store in a Set | |
words = File.foreach(ARGV.shift).map {|l| l.chomp!}.to_set.freeze |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby19 | |
require 'thread' | |
Thread.abort_on_exception = true | |
def log(msg) | |
printf "%-30p %s\n", Thread.current, msg | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HexNum < Numeric | |
# Create a new instance from an int or String. | |
def initialize(val) | |
case val | |
when String | |
@i = parse_string(val) | |
@s = val.frozen? ? val : val.dup.freeze | |
when Numeric | |
@i = val.to_i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby19 | |
require 'benchmark' | |
REP = 10 | |
BS = 1024 | |
files = [] | |
at_exit do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby19 | |
ADDR = %r{[-.\w]+@[-.\w]+} | |
email = DATA.read.freeze | |
# 1. Java style | |
subst = {} | |
puts email.gsub(ADDR) {|match| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby19 | |
DirDfs = Struct.new :root, :state_file do | |
def self.resume(state_file) | |
File.open(state_file, "rb") do |io| | |
Marshal.load(io) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! ruby19 | |
require 'pp' | |
Converter = Struct.new :root, :converted do | |
def convert | |
@stack = [] | |
self.converted = {} | |
dfs(root) |
OlderNewer