Skip to content

Instantly share code, notes, and snippets.

@lpar
lpar / godel.rb
Created November 3, 2011 14:05
Simple Gödel encoding and decoding
#!/usr/bin/env ruby
# encoding: UTF-8
#
# Simple Gödel encoding
# Ruby 1.9.3
require 'prime'
# Allowed characters
CHARS = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@lpar
lpar / SubClass.java
Created November 15, 2011 21:58
Java field initialization weirdness with primitive values
public class SubClass extends SuperClass {
protected int myValue = 69;
public SubClass() {
// You might think that field initialization will have been performed before this constructor is executed.
super();
// However, Java doesn't actually perform all the field initialization for this object until this point in the code.
// Note also that there's nothing in the source code to say that initialization happens here.
System.out.println("Subclass constructor continuing");
@lpar
lpar / forkboy.rb
Created December 23, 2011 18:54
Locate files on Mac OS X which have resource forks
#!/usr/bin/env ruby
# encoding: UTF-8
# Utility for Mac OS X to locate files which have resource forks, and which
# therefore might be corrupted by being transferred via a non-Mac filesystem.
require 'find'
if ARGV.length != 1
puts "usage: forkboy <dir>"
@lpar
lpar / atomizer.js
Created January 11, 2012 21:40
Atomizer: Small embeddable JavaScript web client for Atom feeds.
/*jslint browser: true, indent: 2 */
// Atomizer: Small embeddable JavaScript web client for Atom feeds (RFC 4287).
//
// Generates unordered lists or tables which can be inserted anywhere on
// the page using innerHTML.
//
// The Atom feed needs to be on the same hostname, port and protocol as the
// web page, because of browser security policies.
// See <http://en.wikipedia.org/wiki/Same_origin_policy>
@lpar
lpar / index.html
Created January 19, 2012 17:51
Simple JavaScript slideshow - no frameworks, no CSS3, no Flash
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Slideshow demo</title>
<script type="text/javascript" src="slideshow.js"></script>
<style type="text/css">
#current { position: absolute; left: 0px; top: 0px; z-index: 0; }
#next { position: absolute; left: 640px; top: 0px; z-index: 1; }
#slideshow { position: relative; border: solid #2b2b2b 3px; overflow: hidden; }
</style>
@lpar
lpar / example.rb
Created March 23, 2012 22:56
Brief demonstration of using character encodings in Ruby 1.9
#!/usr/bin/env ruby
# encoding: UTF-8
# Notice I set UTF-8 as the default above. As far as I'm concerned there
# are two choices of encoding: UTF-8, and legacy crap. If I need to deal
# with anything else I'll handle it explicitly, as in this example.
# Open an ISO-8859-1 file.
infile = File.open("iso88591.txt", "r:iso-8859-1")
@lpar
lpar / flac2mp3.rb
Created January 26, 2013 22:58
Ruby script I use to convert FLAC files to MP3, and then copy the metadata across.
#!/usr/bin/env ruby
# encoding: UTF-8
# Convert FLAC files to mp3 files, keeping metadata from the FLAC files.
#
# Requires that FLAC (including metaflac) and LAME tools be installed
# and on the path.
#
# Takes any number of .flac files on the command line.
@lpar
lpar / reader2evernote.rb
Created March 14, 2013 20:35
Google Reader to Evernote. Quick hack together of a Ruby script which will pull all your Google Reader starred items into an Evernote notebook in ENML (Evernote export format).
#!/usr/bin/env ruby
# encoding: UTF-8
# Google Reader to Evernote
# Quick hack together of a Ruby script which will pull all your Google Reader
# starred items into an Evernote notebook in ENML (Evernote export format).
# Requires Ruby 2.0, no other special dependencies. Should work on 1.9 but I
# haven't tested it.
#
@lpar
lpar / ldapauth.rb
Created June 7, 2013 14:53
Example of LDAP authentication in Ruby. Written for IBM Intranet, but should easily be adaptable to other environments.
#!/usr/bin/ruby
# An example of BluePages / IBM Intranet Password authentication using Ruby.
# Uses the gem ruby-ldap, a Ruby wrapper for OpenLDAP. Works with Ruby 2.0.
#
# To get this code to work, you must
#
# 1. gem install ruby-ldap
# 2. add
#
@lpar
lpar / logreport.rb
Created June 7, 2013 19:04
Report the first 10 lines of all files on the command line, transparently decompressing .xz compressed files.
#!/usr/bin/ruby
# encoding: UTF-8
# The audit team wanted to know that we were doing logging as required. As
# evidence, they asked for a regular report consisting of the first 10 lines
# of each daily log file. I wrote this script to automate the process.
# If you want the last 10 lines instead, I suggest the Ruby Gem called Elif,
# which wraps any IO object to read line by line backwards.
LINES_OF_LOG = 10