Skip to content

Instantly share code, notes, and snippets.

View mbklein's full-sized avatar

Michael B. Klein mbklein

  • Northwestern University
  • Evanston, IL
View GitHub Profile
module Kernel
alias_method :debug_original_require, :require
@@load_order_logger = nil
@@load_order_indent = ''
def self.load_order_logger
@@load_order_logger
end
196 @sing
181 @ana
93 @quote
91 @karma
72 @who
43 @decide
32 @band
29 @dunno
27 @rate
27 @love
@mbklein
mbklein / nokogiri_equality.rb
Created February 17, 2011 04:01
Adds Nokogiri::XML::Node#semantically_equal?(node) to test XML documents for semantic equality
require 'nokogiri'
module Nokogiri
module XML
# Attribute nodes are semantically equal if their names and values match exactly
class Attribute
def compare_semantics(node, opts)
xmlr = XmlRegistry.new
xmlr.template(:person) do |xml,name,title|
xml.person :title => title, name
end
doc = Nokogiri::XML('<people/>')
doc.root.add_child(xmlr.person('Jeff Lebowski','The Dude'))
doc.to_xml
=> "<people><person title=\"The Dude\">Jeff Lebowski</person></people>"
#!/bin/bash
framework=$1
project=$2
remote=$3
if [[ ! ("$#" -eq 3)]]
then
echo >&2 "Usage: $0 <framework-repo> <project-name> <project-repo>"
exit 1
@mbklein
mbklein / .bash_aliases
Created August 10, 2011 18:07
BASH aliases for colorized output
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias ls='ls -G'
<?xml version="1.0" encoding="UTF-8"?>
<object PID="druid:bm891ts1074">
<objectType>item</objectType>
<tag>MDForm : ms</tag>
<tag>Project : Manuscripts</tag>
<otherId name="uuid">39ec8596-30b8-5b26-231e-41583ae3d3bf</otherId>
<objectId>druid:bm891ts1074</objectId>
<objectCreator>DOR</objectCreator>
<descMetadata>
<title>Manuscript Title</title>
<add>
<doc>
<field name="id">druid:bm891ts1074</field>
<field name="title">Manuscript Title</field>
<field name="workflow">accessionWF</field>
<field name="workflow">digitizationWF</field>
<field name="status">completed</field>
<field name="status">waiting</field>
<field name="status">error</field>
<field name="status_wps">accessionWF:start-accession:completed</field>
@mbklein
mbklein / bytestring_inflector.rb
Created September 28, 2011 20:54
Convert number of bytes to human-readable string, reducing order of magnitude
BYTE_ORDERS = ['B','KB','MB','GB','TB','PB','EB']
class Numeric
def bytestring
order = 0
while (self > 1024 ** (order+1))
order += 1
end
"%.1f %s" % [self.to_f / (1024 ** order), BYTE_ORDERS[order]]
end
end
@mbklein
mbklein / config_block_explanation.rb
Created November 16, 2011 21:59
Why does key = value work in some cases, but not others?
c = Confstruct::Configuration.new(:foo => {})
c.configure do |c1|
# the block expects one param, so we've remained bound to
# the outer scope and yielded c.
# IOW, self == whatever self was outside the block, and c1 == c.
c1.x 123 # calls c1.method_missing(:x, 123)
c1.y = 456 # calls c1.method_missing(:y=, 123)
c1.foo.bar = 789 # calls ca.foo.method_missing(:bar=, 789)
end