Skip to content

Instantly share code, notes, and snippets.

View asaaki's full-sized avatar
🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki

Christoph Grabo asaaki

🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki
View GitHub Profile
@asaaki
asaaki / tap_with_conditional_return.rb
Created February 10, 2016 14:20
Poor Man's TapWithConditionalReturn
[some_data.fetch('a_string', fallback_string)].reduce(replacer) { |r, s| s == criteria ? r : s }
# or a way simpler workaround:
(origin = some_data.fetch('a_string', fallback_string)) == criteria ? replacer : origin
@asaaki
asaaki / challenge.md
Last active November 27, 2015 12:47
FastCrossing (code challenge)

N people come to a river in the night. There is a narrow bridge, but it can only hold two people at a time. The have one torch and, because it's night, the torch has to be used when crossing the bridge. Each person takes a variable amount of time to cross.

When two people cross the bridge together, they must move at the slower person's pace.

The question is, how fast can you get N people across the bridge?

(Found via ).

@asaaki
asaaki / delete-merged-branches.sh
Created November 11, 2015 16:04
Delete all merged branches
# Excludes the active branch + master and production
git branch --merged | \
grep -v -e "\*" -e master -e production | \
xargs -n1 git branch -d
@asaaki
asaaki / m💩.rb
Last active August 16, 2021 12:18
M💩.🔫 #=> 💥
module M💩
module_function
def 🔫
puts '💥'
end
end
M💩.🔫
@asaaki
asaaki / jemalloc.sh
Last active September 19, 2015 14:22
jemalloc + ruby
# path to the jemalloc shared lib
LD_PRELOAD=/usr/lib/libjemalloc.so bundle exec rails server
# Also see: https://github.com/kzk/jemalloc-rb/issues/3
@asaaki
asaaki / thread_wait_lock.rb
Last active September 5, 2015 15:03
thread_wait_lock.rb
# Condensed from http://blog.arkency.com/2015/09/testing-race-conditions/
wait_lock = true
threads = 5.times.map do |i|
Thread.new do
:noop while wait_lock # shortest no-op loop with break. Nice!*
begin
puts "Hooray from thread #{i}!" # Your awesome thread business logic here
@asaaki
asaaki / absolute.sh
Created August 28, 2015 14:58
Absolute final directory path of origin script
#!/bin/sh
# Shamelessly stolen from:
# https://github.com/michaeldfallen/git-radar/blob/master/git-radar#L7-L13
if [[ "$OSTYPE" == *darwin* ]]; then
READLINK_CMD='greadlink'
else
READLINK_CMD='readlink'
fi
@asaaki
asaaki / Dockerfile
Last active August 29, 2015 14:26
Failed attempt: Building rustc for musl target
FROM nfnty/arch-mini
RUN pacman --sync --noconfirm --refresh --sysupgrade && \
find /var/cache/pacman/pkg -mindepth 1 -delete
RUN pacman --sync --noconfirm \
autoconf automake clang cmake cmake curl diffutils git grep llvm llvm-libs \
m4 make make ncurses python2 sed subversion vim wget which && \
find /var/cache/pacman/pkg -mindepth 1 -delete
defmodule SigilZ do
defmacro sigil_z(input, flags) do
quote do
[input: unquote(input), flags: unquote(flags)]
end
end
end
# then:
@asaaki
asaaki / hash_ext.rb
Created May 12, 2015 13:45
Ruby Hash extension: .subhash(*keys)
# Stolen from <http://stackoverflow.com/a/9025552/653173>
module HashExt
def subhash(*keys)
keys = keys.select { |k| key?(k) }
Hash[keys.zip(values_at(*keys))]
end
end