Skip to content

Instantly share code, notes, and snippets.

View Burgestrand's full-sized avatar
🙃
(:

Kim Burgestrand Burgestrand

🙃
(:
View GitHub Profile
@Burgestrand
Burgestrand / mooer.coffee
Created April 25, 2013 16:00
Coffee script annotated output
var Kitten, Mooer, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function (child, parent)
{
for (var key in parent)
{
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor()
@Burgestrand
Burgestrand / sass.rb
Created April 17, 2013 05:53
An FFI binding for libsass for a Got.rb meetup, libsass at https://github.com/hcatlin/libsass
require "ffi"
module Sass
extend FFI::Library
ffi_lib "sass"
module MemoryString
extend FFI::DataConverter
native_type FFI::Type::POINTER
@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?
@Burgestrand
Burgestrand / semaphore.rb
Created April 3, 2013 10:43
A super-simple semaphore implementation in Ruby
# coding: utf-8
require 'thread'
class Semaphore
def initialize(size = 1)
@queue = SizedQueue.new(size)
size.times { inc }
end
def inc
@Burgestrand
Burgestrand / livereload.md
Last active December 11, 2015 07:58
How to enable livereload.js for a rails application from scratch

Livereload

Name assets with their final extension, e.g.

  • application.js.coffee
  • application.css.scss

… or guard-livereload won’t pick them up properly.

Gems

gem "guard-livereload"
@Burgestrand
Burgestrand / distance.rb
Created December 22, 2012 05:39
Calculate the operations needed to transform one list into another
require "set"
class Distance
def initialize(target)
@target = target.dup
end
attr_reader :target
def calculate(origin)
@Burgestrand
Burgestrand / whitespace.rb
Created August 30, 2012 07:13
A whitespace thingy in Ruby, just for the lulz
# coding: utf-8
class Whitespace
def initialize(&block)
@data = 0
@pos = -1
instance_eval(&block)
end
def  
tap { @pos += 1 }
@Burgestrand
Burgestrand / Gemfile
Created August 10, 2012 09:38
Threaded scraping with Capybara, Webkit and Celluloid
source :rubygems
gem 'pry'
gem 'capybara'
gem 'capybara-webkit'
gem 'celluloid'
@Burgestrand
Burgestrand / sidekiq_terminate_jobs_middleware.rb
Created April 3, 2012 09:23
A Sidekiq middleware for terminating long-running jobs when shutdown is requested
class TerminateJobs
class Terminate < StandardError
end
include Sidekiq::Util
def call(worker, msg, queue)
current_thread = Thread.current
old_signals = {}
# 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!