Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / gist:7518
Created August 27, 2008 16:07
git tests to skip on Solaris Nevada.
# git tests to skip on Solaris Nevada (mainly iconv-related).
make test GIT_SKIP_TESTS='t3900 t3901 t5100 t9301'
@mudge
mudge / gist:14328
Created October 2, 2008 09:44
Display orphan gems (those that are not depended upon).
#!/usr/bin/env ruby
gems = {}
`gem dependency -R`.split("\n\n").each do |gem|
unless gem["Used by"]
version = gem[/\d+\.\d+\.\d+$/]
name = gem[4..-1][/^.*(?=-\d\.)/]
(gems[name] ||= []) << version
end
end
@mudge
mudge / gist:17385
Created October 17, 2008 09:57
Parser for the jQuery Tablesorter Plugin to sort European dates
/* Parser for the jQuery Tablesorter Plugin to sort European dates, e.g. 30/02/08 */
$.tablesorter.addParser({
id: 'dates',
is: function(s) { return false },
format: function(s) {
var dateArray = s.split('/');
return "20" + dateArray[2] + dateArray[1] + dateArray[0];
},
type: 'numeric'
});
@mudge
mudge / application_helper.rb
Created November 7, 2008 14:37
Widon't helpers for Rails (both the original and 2.1 versions).
module ApplicationHelper
# Shaun Inman's original Widon't
# http://shauninman.com/archive/2006/08/22/widont_wordpress_plugin
#
# @param [String] text the text to apply Widon't to
# @return [String] the text with Widon't applied
def widont(text)
text.strip!
text[text.rindex(' '), 1] = '&nbsp;' if text.rindex(' ')
@mudge
mudge / application_helper.rb
Created November 7, 2008 14:40
Maruku helper for Rails.
# For Rails 2.3.2 or earlier.
require 'maruku' # or use config.gem "maruku" in environment.rb
module ApplicationHelper
# Replacement for Rails' default Markdown helper which uses Maruku instead
# of BlueCloth.
def markdown(text)
text.blank? ? "" : Maruku.new(text).to_html
end
@mudge
mudge / .htaccess
Created November 7, 2008 14:48
Remove file extensions in URLs with mod_rewrite but preserve 404 errors.
# The following will allow you to use URLs such as the following:
#
# example.com/anything
# example.com/anything/
#
# Which will actually serve files such as the following:
#
# example.com/anything.html
# example.com/anything.php
#
@mudge
mudge / gist:22878
Created November 7, 2008 14:57
JavaScript function to replicate Ruby's "succ()" method.
String.prototype.succ = function() {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var result = '';
for (var i = 0; i < this.length; i++) {
if (alphabet.indexOf(this.charAt(i).toLowerCase()) != -1) {
if (this.charAt(i).isUpperCase()) {
result += alphabet.charAt(
alphabet.toUpperCase().indexOf(this.charAt(i)) + 1).toUpperCase();
} else {
result += alphabet.charAt(alphabet.indexOf(this.charAt(i)) + 1);
@mudge
mudge / gist:78519
Created March 13, 2009 10:40
A challenge set by sross. (See his Lisp solution: http://gist.github.com/79799)
# Ensure that all the arrays in a hash are ordered the same (even if
# they have missing elements).
def ordered?(hash_of_arrays)
hash_of_arrays.all? do |k,v|
hash_of_arrays.all? { |k2,v2| v - (v - v2) == v2 - (v2 - v) }
end
end
ordered?({ 1 => [1, 2, 3, 4], 2 => [1, 3, 4], 3 => [2, 4, 5], 4 => [1, 2, 4, 6]})
#=> true
@mudge
mudge / gist:104527
Created April 30, 2009 16:25
Convenience method for removing patterns from strings.
class String
# "Hello says Bob" - "Bob"
#=> "Hello says "
# "Hello says Bob" - /l+/
#=> "Heo says Bob"
#
# @param [String, Regexp] pattern either a string or regular expression to remove from the string.
# @return [String] a copy of the string with the pattern remvoed
def -(pattern)
@mudge
mudge / gist:108051
Created May 7, 2009 10:52
Replace non-ASCII characters with their decimal-encoded XML entity.
# Replace non-ASCII characters with their decimal-encoded XML entity.
str.gsub!(/[^\x00-\x7f]/) { |s| "&##{s.unpack("U")[0]};" }