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 '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) |
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 'date' | |
class EndlessEnum | |
include Enumerable | |
def initialize(x, method = :succ) | |
@x = x | |
@method = method |
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
# This class works as a proxy for an instance | |
# and presents it as Hash. | |
class InstanceHash | |
include Enumerable | |
def initialize(obj, default_val = nil, &default_proc) | |
@obj = obj | |
@default = default_val | |
@default_proc = default_proc || lambda {|h,k| @default} | |
end |