Skip to content

Instantly share code, notes, and snippets.

View bxt's full-sized avatar

Bernhard Häussner bxt

View GitHub Profile
@bxt
bxt / with_last.rb
Created October 6, 2017 13:26
Map over ruby arrays / enumerables and have the last element marked with a boolean
def with_last(enumerable, length = enumerable.length)
Enumerator.new do |yielder|
enumerable.each_with_index do |element, index|
yielder.yield(element, index == length - 1)
end
end
end
@bxt
bxt / .block
Created July 28, 2017 08:54 — forked from mbostock/.block
Collision Detection
license: gpl-3.0
@bxt
bxt / forktest.cr
Last active May 14, 2017 00:02
Fork test in Crystal lang
puts "Starting..."
forks = 10.times.map do |i|
fork do
puts "In fork #{i}"
sleep(i)
puts "End fork #{i}"
end
end.to_a
@bxt
bxt / xml-to-csv.rb
Created April 26, 2017 22:53
Convert XML to CSV file using Ruby (nokogiri)
require 'csv'
require 'nokogiri'
filename = "timeentries"
doc = File.open("#{filename}.xml") { |f| Nokogiri::XML(f) }
entries = doc.css('time-entry')
CSV.open("#{filename}.csv", "wb") do |csv|
@bxt
bxt / encapsulation-problem.rb
Last active December 7, 2016 08:57
Ruby's top level methods break encapsulation
def a_random_method_not_defined_in_a_class
puts self.class # outputs ExampleClass
puts @a_private_member # works
end
class ExampleClass
def initialize(a_private_member)
@a_private_member = a_private_member
end
@bxt
bxt / make-git-great-again.sh
Last active November 16, 2016 12:49
Find out what coders want to make great again
git log -i --format=oneline | sed -n '/.*[Mm]ake \(.*\) great again.*/ s//\1/p'
# Or even:
git log -i --format=oneline | sed -n '/.*[Mm]ake \(.*\) great again.*/ s//\1/p' | ruby -ne 'BEGIN{a=[]};a<<$_.strip;END{puts a.join(", ")}'
# A bit shorter and maybe more performant (so use this if you want to make many things great again, e.g. the whole of america):
git log -i --format=oneline | sed -n '/.*[Mm]ake \(.*\) great again.*/ s//\1/p' | ruby -ne 'print ", " if $.!=1;print $_.strip;END{puts}'
@bxt
bxt / keep-every-50th-file.sh
Created November 9, 2016 23:13
Keep only 1/50 of all files in a directory, remove others forever
ls | awk 'NR%50!=25' | xargs rm
@bxt
bxt / gitlab-openmr.sh
Last active August 1, 2017 18:15
Open the merge request for a merged commit in GitLab
#!/bin/bash
set -e
MAIN=${2:-development}
OPEN=open
MRS=$(git remote show -n origin | sed -n '/^ Fetch URL: git@\(.*\):\(.*\)\.git$/ s//https:\/\/\1\/\2\/merge_requests\//p')
if [ -z "$1" ]
@bxt
bxt / named-parameters.js
Created October 31, 2016 04:01
Javascript (ES6) Named parameters with default values ツ
// I just figured out you could do this in ES6:
function foo ({ a = 3, b = 5 } = {}) {
console.log(a, b);
}
foo() // => 3 5
foo({a: 0}) // => 0 5
foo({b: 0}) // => 3 0
foo({a: 4, b: 6}) // => 4 6
@bxt
bxt / schuhe.md
Last active May 3, 2016 21:35
Halten teure Schuhe länger als billige? - Schukäufe, Preise, Tragen und Abnutzung.

Halten teure Schuhe länger als billige?

Schuhkäufe

  • g: 04.11.12, 60€, Grüne Converse Sneakers
  • b: 08.01.14, 13€, Blaue YouTurn Sneakter
  • r: 27.02.15, 45€, Rote Converse Sneakers
  • w: 09.03.15, 20€, Weiße Converse Chucks