Skip to content

Instantly share code, notes, and snippets.

@urbanautomaton
urbanautomaton / dci_benchmark.rb
Last active December 10, 2015 12:48
Attempt to separate out method call cost in DCI benchmark
require 'rubygems'
require 'delegate'
require 'benchmark'
include Benchmark
class ExampleClass
def foo; 42; end
end
require 'rspec'
class Thing; end
describe "change matcher" do
it "behaves counterintuitively with classes" do
thing = nil
expect {
thing = Thing
}.to change {
@urbanautomaton
urbanautomaton / extend_monkeypatch.rb
Last active December 10, 2015 20:08
Method cache? What method cache?
class Object
alias_method :orig_extend, :extend
def extend(mod, *args)
puts "Extending #{self.class} with #{mod}"
orig_extend(mod, *args)
end
end
#unverified.list obtained from https://gist.github.com/raw/865b05630be54544ca72/a1a0c114d5eab0e65efccc941ca013fd06e6f43c/unverified.list
File.readlines("unverified.list").map do |line|
gem_file = line.split.last.gsub("./", "")
m = gem_file.match(/(.*)-((?:\d+.)*\d+.*)\.gem/)
[m[1], m[2]]
end.sort.uniq.each do |gem, version|
in_gemfile_lock = system %Q{grep -q "#{gem} (#{version})" Gemfile.lock}
puts "Unsafe #{gem} (#{version})" if in_gemfile_lock
end
$ bash -x powerline.sh left
++ dirname powerline.sh
+ export TMUX_POWERLINE_DIR_HOME=.
+ TMUX_POWERLINE_DIR_HOME=.
+ source ./config/helpers.sh
+ source ./config/paths.sh
++ export TMUX_POWERLINE_DIR_LIB=./lib
++ TMUX_POWERLINE_DIR_LIB=./lib
++ export TMUX_POWERLINE_DIR_SEGMENTS=./segments
++ TMUX_POWERLINE_DIR_SEGMENTS=./segments
@urbanautomaton
urbanautomaton / enumerator.rb
Last active December 14, 2015 15:19
jruby destructuring oddity
pairs = [[1,1],[2,2],[3,3]]
enumerator = Enumerator.new do |y|
pairs.each { |x| y.yield(x) }
end
puts "Array:"
puts pairs.find {|a,b| a > 2}.inspect
puts pairs.find {|a,b| b > 2}.inspect
@urbanautomaton
urbanautomaton / git_ps1_fast.sh
Last active December 15, 2015 05:09
Faster version of __git_ps1 if you only want current branch
#!/bin/bash
function git_ps1_fast() {
local dir="$PWD"
local git_dir
until [[ -z "$dir" ]]; do
git_dir="$dir/.git"
if [[ -d "$git_dir" ]]; then
echo " (`git rev-parse --abbrev-ref HEAD`)"
@urbanautomaton
urbanautomaton / wtf.rb
Last active December 19, 2015 22:58
Fuck Rails const autoloading
# lib/a.rb
module A
end
# lib/b.rb
class B
end
# $ rails console
# > A.const_missing("B")
@urbanautomaton
urbanautomaton / doc.markdown
Last active December 21, 2015 07:29
Git: interactive rebase autosquash helpers

Update: as I suspected, this is a stupid exercise in wheel-reinventing, and I could've done git commit --fixup=<commitish> all along. Thanks, Mudge.

For interactive rebase and autosquash background, see here.

All this helper does is construct commit messages that will be picked up by autosquash, using a commitish reference so you don't have to do the message yourself. If we know we have some changes that should've gone in the commit before last, and we don't want to add to its commit message, our workflow is now:

  1. make changes
  2. stage changes
  3. $ fixup HEAD^
  4. $ git rebase -i --autosquash when ready
method=POST path=/training_sessions format=api_v1 controller=training_sessions action=create status=201 duration=738.18 view=303.25 db=0.00 params={"workout_duration"=>nil, "workout_name"=>nil, "training_session"=>{"workout_id"=>nil, "post_to_twitter"=>1, "started_at"=>"12 Sep 2013", "content"=>"Cycle to work", "map_visibility"=>"public", "photos_attributes"=>{}, "training_logs_attributes"=>[{"distance"=>1.526738, "time_minutes"=>5, "time_hours"=>0, "time_seconds"=>33, "raw_data"=>"[FILTERED]", "amount"=>nil, "exercise_id"=>nil, "route_name"=>nil, "distance_unit"=>"km", "weight"=>nil, "weight_unit"=>"kg"}], "visibility"=>"public", "post_to_facebook"=>1}, "workout_difficulty"=>nil}
method=POST path=/training_sessions format=api_v1 controller=training_sessions action=create status=201 duration=322.64 view=53.30 db=0.00 params={"workout_duration"=>nil, "workout_name"=>nil, "training_session"=>{"workout_id"=>nil, "post_to_twitter"=>1, "started_at"=>"12 Sep 2013", "content"=>nil, "map_visibility"=>"public", "photo