Skip to content

Instantly share code, notes, and snippets.

View jsborjesson's full-sized avatar

Jimmy Börjesson jsborjesson

View GitHub Profile
# This is a place where you can put code that should be executed once,
# and accessed by several different threads. When it is called, it will
# block the thread until the calculation has been completed, and memoize it.
#
# This means all threads that call the same method will wait for the same result,
# that is only carried out once. Think of it as a traffic light; cars arrive at the
# red light at different times, wait, and all take off together once it turns green.
class CrossThreadMemoizer
def initialize(callable)
@callable = callable
def x(one: 1, two: 2)
local_variables.each_with_object({}) do |var, memo| memo[var] = eval(var.to_s) end
end
@jsborjesson
jsborjesson / git-remote-rename.sh
Created April 28, 2015 16:10
Recursively updates git remotes when you've renamed an organization
#!/bin/sh
for i in $(find $PWD -maxdepth 1 -type d); do
cd $i
if [ -d .git ]; then
remotes=$(git remote -v)
if [[ "$remotes" =~ [email protected]:myorg ]]; then
git remote set-url origin [email protected]:myneworg/$(basename `git rev-parse --show-toplevel`)
fi
fi
cd ..
@jsborjesson
jsborjesson / ntfs.sh
Created June 11, 2014 18:56
Mount NTFS drive on OSX
# Create or edit /etc/fstab/ to contain this:
LABEL=VOLUME_NAME none ntfs rw,auto,nobrowse
@jsborjesson
jsborjesson / inline_ruby.vim
Last active August 29, 2015 14:01
Execute selected ruby into inline comments
" Inline ruby interpretation, inspired by RubyTapas.
"
" Example output when run over the top line:
"
" 3.times { |num| puts num }
" # => 0
" # => 1
" # => 2
"
vmap <leader>r :!tee >(cat) \| ruby \| sed 's/^/\\# \=> /'<cr>
module Typoglycemia
def self.convert(text)
text.gsub(/(\w+)/) { |word| scramble_middle_characters(word) }
end
def self.scramble_middle_characters(word)
return word if word.length < 4
word[0] + shuffle_letters(word[1...-1]) + word[-1]
end
IO.readlines("/usr/share/dict/words").map(&:strip) # array of english words
@jsborjesson
jsborjesson / register_statistics.rake
Created April 14, 2014 09:46
Add more folders to rake stats
task :register_statistics do
require 'rails/code_statistics'
STATS_DIRECTORIES << ['Services', 'app/services']
STATS_DIRECTORIES << ['Services Tests', 'test/services']
CodeStatistics::TEST_TYPES << 'Services Tests'
end
Rake::Task['stats'].enhance [:register_statistics]
@jsborjesson
jsborjesson / test_helper.rb
Last active August 29, 2015 13:58
Minitest Spec for Rails.
require 'simplecov'
SimpleCov.start 'rails'
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'rails/test_help'
require 'factories'
@jsborjesson
jsborjesson / json_for_strategy.rb
Created March 18, 2014 09:04
Easy testing of json API:s with Factory Girl
class JsonForStrategy
def initialize
@strategy = FactoryGirl.strategy_by_name(:build).new
end
delegate :association, to: :@strategy
def result(evaluation)
@strategy.result(evaluation).to_json
end