Skip to content

Instantly share code, notes, and snippets.


Troels Knak-Nielsen troelskn

View GitHub Profile
troelskn / dk-tax.js
Last active Dec 19, 2019
Danish tax calculations for Google sheets
View dk-tax.js
function DK_TAX_PARSE_OPTIONS(opts) {
var options = {
hasChildren: true, // Do you receive "børnepenge"?
isFamily: true, // Are you married (and your spouse without income)?
interests: 44256, // Deductible interests paid (typically on a mortgage)
churchTax: 0.00867, // Set your church tax rate here, if you are a member of the church. Otherwise set to 0.0
municipalityTaxRate: 0.24926 // Find you tax rate here:
var applyOption = function(key, value) {
var key = key.trim();
View table_print.rb
# takes an array of hashes and prints as a table on the console
def table_print(rows)
table = [] + { |v| }
sizes = { |row| }
puts { |row| { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n")
troelskn / ruby_print.rb
Created Apr 26, 2019
Ruby pretty print with rubocop standard syntax
View ruby_print.rb
class RubyPrint
def self.pp(mixed)
puts new.transform(mixed)
def transform(mixed, indentation = 1)
if mixed.is_a?(Hash)
transform_hash mixed, indentation
elsif mixed.is_a?(Array)
View just_and_exactly.rb
# frozen_string_literal: true
require "active_record"
module JustAndExactly
module ActiveRecordExtensions
MultipleRecordsFound =
# Like +first!+, but will also fail if the query would match more than
# one record.
View docker-machine-auth
PS1='\[\e[1m\]\[\e[34m\]\u@\h$([ "$DOCKER_MACHINE_NAME" = "" ] || echo " \[\e[33m\][docker-machine:$DOCKER_MACHINE_NAME]\[\e[34m\]"): \[\e[35m\]\w\[\e[33m\]$(__git_ps1) \[\e[34m\]$\[\e[0m\] '
docker-machine-auth () {
if [ "$MACHINE_NAME" = "" ]
eval "$(docker-machine env -u)"
eval "$(docker-machine env $MACHINE_NAME)"
troelskn / db.rake
Last active Jan 28, 2019
Wait for db. Useful in a dockerized setup
View db.rake
namespace :db do
desc "Wait for db. Used for initial boot up"
task wait: :environment do
puts "*** Giving DB time to boot"
catch :done do
10.times do
Timeout.timeout(1) do
ActiveRecord::Base.connection.execute("SELECT NOW()")
troelskn / shell_execute.rb
Created Oct 24, 2018
Running a shell subprocess is surprisingly hard in ruby
View shell_execute.rb
require "open3"
def execute_shell(command, verbose: false) "[SHELL] #{command}"
# see:
# see:
output = []
Open3.popen3(command) do |stdin, stdout, stderr, thread|
# read each stream from a new thread
troelskn / sprockets.rb
Created Nov 1, 2017
Use sprockets-es6 to handle jsx
View sprockets.rb
# config/initializers/sprockets.rb
module Sprockets
if respond_to?(:register_transformer)
register_mime_type 'text/jsx', extensions: ['.jsx'], charset: :unicode
register_transformer 'text/jsx', 'application/javascript', ::BabelTransformer
register_preprocessor 'text/jsx', DirectiveProcessor
if respond_to?(:register_engine)
args = ['.jsx', ::BabelTransformer]
troelskn / multipart.rb
Created Oct 27, 2017
Low level implementation of multipart post, using net/http
View multipart.rb
# Low level implementation of multipart post, using net/http
class MultipartPost
def initialize(uri:)
@uri = uri
@post_body = []
def send
request =
request.body = build_body
troelskn / Rakefile
Created Oct 6, 2017
Drop into an IRB session from within a rake task
View Rakefile
desc "Start an interactive console"
task :console do
require "irb"
require "irb/completion"
require "pp"
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
irb =
You can’t perform that action at this time.