Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / test.retry.php
Created July 8, 2013 13:27
A retry function for PHP.
<?php
require_once dirname(__FILE__) . '/../lib/simpletest/autorun.php';
function retry($f, $delay = 10, $retries = 3)
{
try {
return $f();
} catch (Exception $e) {
if ($retries > 0) {
sleep($delay);
@mudge
mudge / gist:786953
Last active November 3, 2021 08:59
How to pass blocks between methods in Ruby < 3.0.0 without using &block arguments.
# UPDATE: The ability to call Proc.new without a block was removed in Ruby 3.0.0
# See https://bugs.ruby-lang.org/issues/10499 for more information.
# So you might have heard that this is slow:
#
# def some_method(&block)
# block.call
# end
#
# Compared to:
@mudge
mudge / gist:221616
Created October 29, 2009 17:17
A simple rich text editor with jQuery and iframe designMode.
/*
* A very basic rich text editor using jQuery and iframe designMode.
*
* In your HTML...
* <input type="hidden" id="article_body" name="article[body]">
* <iframe id="paste"></iframe>
*/
/*
* NOTE: The order of writing and setting designMode is very important.
@mudge
mudge / gist:428455
Created June 7, 2010 09:29
Namespaces with Nokogiri::Builder
# Dealing with namespaces in Nokogiri.
#
# First thing you must do is abandon what you're used to with Builder,
# namespaces aren't just attributes on an element, they uniquely identify
# what sort of element you are building and, as such, you are better off
# specifying them manually.
#
# The key here is accessing the Nokogiri::XML::Element being built with
# b.parent, then you can set the namespace (distinguished by an element
# with a prefix such as "soap12:Envelope") or add a default namespace
@mudge
mudge / pythagorean_means.rb
Last active May 20, 2021 10:55
A Ruby refinement to add methods to Enumerable for calculating the three Pythagorean means.
# A refinement to add methods to Enumerables for calculating the three
# Pythagorean means.
#
# See https://en.wikipedia.org/wiki/Pythagorean_means
module PythagoreanMeans
# Note that due to a bug refining modules in Ruby 2.7 [1], we can't `refine
# Enumerable` so we `refine Array` instead.
#
# See also https://interblah.net/why-is-nobody-using-refinements
@mudge
mudge / README.md
Last active May 11, 2021 19:45
Compiling & installing cloudflared for DNS-over-HTTPS on a Raspberry Pi Model B
@mudge
mudge / pdftk.rb
Created April 23, 2021 13:16
Homebrew Cask for PDFtk
cask "pdftk" do
version "2.02"
sha256 "c33cf95151e477953cd57c1ea9c99ebdc29d75f4c9af0d5f947b385995750b0c"
url "https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-#{version}-mac_osx-10.11-setup.pkg"
name "PDFtk"
desc "Tool for doing everyday things with PDF documents"
homepage "https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/"
pkg "pdftk_server-2.02-mac_osx-10.11-setup.pkg"
@mudge
mudge / EachCons.php
Created July 17, 2014 14:32
Iterate over an array as a sliding window of element pairs.
<?php
$a = array(1, 2, 3);
$pairs = array_map(null, $a, array_slice($a, 1));
/**
* Prints:
* (1,2)(2, 3)(3, )
*/
foreach ($pairs as list($x, $y)) {
echo "({$x}, {$y})";

Keybase proof

I hereby claim:

  • I am mudge on github.
  • I am mudge (https://keybase.io/mudge) on keybase.
  • I have a public key ASBDGnU-C3RqiI8Qhc2PUEsdVcNTtf5avLN5urIUcEbzOwo

To claim this, I am signing this object:

@mudge
mudge / auto.sh
Created August 26, 2019 09:18
Auto-switch Node.js versions with chnode by reading .node-version files
# Based off chruby's auto.sh: https://github.com/postmodern/chruby#auto-switching
unset NODE_AUTO_VERSION
function chnode_auto() {
local dir="$PWD/" version
until [[ -z "$dir" ]]; do
dir="${dir%/*}"
if { read -r version <"$dir/.node-version"; } 2>/dev/null || [[ -n "$version" ]]; then