Skip to content

Instantly share code, notes, and snippets.

View Burgestrand's full-sized avatar
🙃
(:

Kim Burgestrand Burgestrand

🙃
(:
View GitHub Profile
@Burgestrand
Burgestrand / Procfile.dev
Last active April 5, 2024 08:32
A custom livereload using Turbo 8 morph, Turbo Stream, and Listen.
cable: WEB_CONCURRENCY=0 PORT=28080 bundle exec puma config/cable.ru
@Burgestrand
Burgestrand / select-server.php
Created October 2, 2010 13:40
Naive server using socket_select() in PHP
<?php
// Config
$host = '127.0.0.1';
$port = 0; // Random port
// Setup
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$clients = array();
// Bind, listen and disable blocking
@Burgestrand
Burgestrand / download-progress.rb
Created June 27, 2010 13:55
Ruby HTTP file download with progress measurement
require 'net/http'
require 'uri'
def download(url)
Thread.new do
thread = Thread.current
body = thread[:body] = []
url = URI.parse url
Net::HTTP.new(url.host, url.port).request_get(url.path) do |response|
@Burgestrand
Burgestrand / webapp.rb
Created January 25, 2011 03:06 — forked from igrigorik/webapp.rb
Turn any ruby Object into a web application!
require 'rack'
class Object
def to_webapp
def self.call(env)
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func || :inspect, *attrs)]
end
self
end
@Burgestrand
Burgestrand / retry.rb
Created April 3, 2013 10:55
Retry a block a given number of times if necessary, protecting against a whitelist of exceptions
class Fixnum
# Runs a block, catching any exceptions matching the exceptions in the
# input parameters. After +self+ number of tries, the exception is
# re-raised and the attempts will terminate.
#
# @yield +self+
# @param [Exception, …] exs Exceptions that will be caught and recovered from.
# @return Whatever the block returns.
def tries(*exs)
exs = [StandardError] if exs.empty?
superman 127.0.0.1 Kim whoami
database 127.0.0.1 Kim sleep 1 && echo "Database says Hello!"
localhost 127.0.0.1 Kim date
@Burgestrand
Burgestrand / README.md
Created February 3, 2012 23:20
A ruby script to construct magnet links out of .torrent files

Magneto

It reads your torrents. Spit out magnet URIs.

Example Usage

$ ./magneto.rb magneto.rb.torrent

Results in:

#####################################################################
# BTT Octopus Pin Aliases
# See https://github.com/VoronDesign/VoronUsers/tree/master/firmware_configurations/klipper/revnull/btt_octopus_pins
#####################################################################
[board_pins]
aliases:
# Stepper drivers
MOT0_EN=PF14, MOT0_STEP=PF13, MOT0_DIR=PF12, MOT0_CS=PC4, # MOTOR0
MOT1_EN=PF15, MOT1_STEP=PG0, MOT1_DIR=PG1, MOT1_CS=PD11, # MOTOR1
# Ruby Thread Pool
# ================
# A thread pool is useful when you wish to do some work in a thread, but do
# not know how much work you will be doing in advance. Spawning one thread
# for each task is potentially expensive, as threads are not free.
#
# In this case, it might be more beneficial to start a predefined set of
# threads and then hand off work to them as it becomes available. This is
# the pure essence of what a thread pool is: an array of threads, all just
# waiting to do some work for you!
@Burgestrand
Burgestrand / hub.swift
Last active March 3, 2021 09:35
A statically typed notification center in Swift
import class Foundation.NotificationCenter
import struct Foundation.Notification
/*
Swift does not allow us to have static members of a generic class, which
means we won't be able to use the dot short hand for any method that takes
an Event<T> as parameter.
However, we can work around this by having a superclass, `Events`, and then
inherit it from it in our `Event<T>`. Any static members of `Events` will