Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
# takes an array of hashes and prints as a table on the console
def table_print(rows)
table = [rows.first.keys.map(&:to_s)] + rows.map(&:values).map { |v| v.map(&:to_s) }
sizes = table.map { |row| row.map(&:size) }.transpose.map(&:max)
puts table.map { |row| row.each_with_index.map { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n")
end
@troelskn
troelskn / ruby_print.rb
Created April 26, 2019 09:18
Ruby pretty print with rubocop standard syntax
class RubyPrint
def self.pp(mixed)
puts new.transform(mixed)
end
def transform(mixed, indentation = 1)
if mixed.is_a?(Hash)
transform_hash mixed, indentation
elsif mixed.is_a?(Array)
# frozen_string_literal: true
require "active_record"
module JustAndExactly
module ActiveRecordExtensions
MultipleRecordsFound = Class.new(ActiveRecord::ActiveRecordError)
# Like +first!+, but will also fail if the query would match more than
# one record.
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 () {
MACHINE_NAME=$1
if [ "$MACHINE_NAME" = "" ]
then
eval "$(docker-machine env -u)"
else
eval "$(docker-machine env $MACHINE_NAME)"
fi
}
@troelskn
troelskn / db.rake
Last active January 28, 2019 12:40
Wait for db. Useful in a dockerized setup
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
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute("SELECT NOW()")
@troelskn
troelskn / shell_execute.rb
Created October 24, 2018 14:11
Running a shell subprocess is surprisingly hard in ruby
require "open3"
def execute_shell(command, verbose: false)
Rails.logger.info "[SHELL] #{command}"
# see: https://nickcharlton.net/posts/ruby-subprocesses-with-stdout-stderr-streams.html
# see: http://stackoverflow.com/a/1162850/83386
output = []
Open3.popen3(command) do |stdin, stdout, stderr, thread|
# read each stream from a new thread
@troelskn
troelskn / sprockets.rb
Created November 1, 2017 14:00
Use sprockets-es6 to handle jsx
# 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
end
if respond_to?(:register_engine)
args = ['.jsx', ::BabelTransformer]
@troelskn
troelskn / multipart.rb
Created October 27, 2017 09:16
Low level implementation of multipart post, using net/http
# Low level implementation of multipart post, using net/http
class MultipartPost
def initialize(uri:)
@uri = uri
@post_body = []
end
def send
request = Net::HTTP::Post.new(@uri.request_uri)
request.body = build_body
@troelskn
troelskn / Rakefile
Created October 6, 2017 12:27
Drop into an IRB session from within a rake task
desc "Start an interactive console"
task :console do
require "irb"
require "irb/completion"
require "pp"
ARGV.clear
IRB.setup(nil)
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
irb = IRB::Irb.new
<?php
class IbanConverter
{
function convert_dk($input) {
// DKkk BBBB CCCC CCCC CC
$iban = "DK00" .
str_pad($input['dk_bank_reg_number'], 4, "0", STR_PAD_LEFT) .
str_pad($input['dk_bank_account_number'], 10, "0", STR_PAD_LEFT);
$this->iban_replace_checksum($iban);