Skip to content

Instantly share code, notes, and snippets.

View jah2488's full-sized avatar

Justin Herrick jah2488

View GitHub Profile
@jah2488
jah2488 / seeing-is-believing-sublime-install.sh
Last active August 26, 2015 22:39
curl -sSL http://bit.ly/1EhquUW | bash -s 2.2.3 (Assumes you're running Sublime Text 3 on OS X with RVM)
#!/bin/sh
[ -z "$1" ] && echo "No argument supplied: Your ruby version is required" && exit 1
RUBY_VERSION=$1
PACKAGE_DIR="$HOME/Library/Application Support/Sublime Text 3/Packages/"
gem install seeing_is_believing
rvm wrapper "$RUBY_VERSION" sublime
@jah2488
jah2488 / diff.rb
Last active August 29, 2015 13:55
Non-destructively perform a difference operation on two arrays
def diff(a,b)
hash_by_count(a)
.merge(hash_by_count(b)) { |_, av, bv| av - bv }
.flat_map { |k, v| [k] * v }
end
private
def hash_by_count(arr)
Hash[arr.group_by { |x| x }.map { |k, v| [k, v.count] }]
end
@jah2488
jah2488 / kwrdfun.rb
Created January 31, 2014 15:33
Playing around with ruby's new keyword arguments to improve method clarity
def extract(from_record:, attribute:)
from_record[attribute]
end
def parse(path)
get_data_from(path).map do |record|
output_class.new(*attributes.map { |name| extract(attribute: name, from_record: record) })
end
end
(defn project-link [project]
(when project [:h3 [:a {:href (str "/" (:permaname project) "/project/view")} (str "Project: " (:name project))]]))
@jah2488
jah2488 / openvim.sh
Created February 3, 2014 23:20
Find and Open in Vim Splits
vim -o `git status --porcelain | awk '{print $2}'` # Open all modified files from git in a different vim split
vim -o `ack -l $SEARCHTERM` # Search for all files that contain a given pattern and open them in vim splits
@jah2488
jah2488 / rb_vs_sql.rb
Created February 6, 2014 15:17
Rough benchmarking of running the (roughly) equivalent sorting code in ruby vs sql
require 'active_record'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection 'sqlite3:///:memory:'
ActiveRecord::Schema.define do
create_table(:posts) { |t| t.string :name }
create_table(:comments) { |t| t.integer :post_id }
end
@jah2488
jah2488 / module_madness.rb
Last active August 29, 2015 13:56
Module Constant oddity
module Mod
A = :a
const_set('B', :b)
end
Mod.module_exec do
C = :c #This seems to just vanish entirely, but its actually being assigned elsewhere. See comments below.
const_set('D', :d)
end
Mod::A #=> :a
@jah2488
jah2488 / namespace.rb
Last active August 29, 2015 13:56
A simple ruby method to cleanup nested module setup for namespacing.
def ns(namespace, delim = '.', &block)
nest_mod(namespace.split(delim), block)
end
def nest_mod(mod = Kernel, module_names, block)
return mod.module_exec(&block) if module_names.empty?
find_or_create_constant_in_module(mod, constantize(module_names.first)).tap do |this|
make_module_methods_accessible(this)
this.module_exec do
nest_mod(this, module_names.drop(1), block)
@jah2488
jah2488 / ral.rb
Last active August 29, 2015 13:56
array of random strings of random length
ral = -> { Array.new(5) { Array.new(1..10).sample.times.map { Array('a'..'z').sample }.join } }
def array_of_random_strings_of_random_length(array_length: 5, string_length: 5, string_range: 'a'..'z')
Array.new(array_length) do
Array(1..string_length).sample.times.map do
Array(string_range).sample
end.join
end
end
#oh the joys of ruby
@jah2488
jah2488 / method_overloading.rb
Last active August 29, 2015 13:56
Hey look, its multi method body(s) in ruby, method overloading really. Now you can define a bunch of methods with different arity and call them from a single place without worry*
class Object
def defn(name, &block)
@name = name
@fns = []
instance_eval(&block)
self.class.send(:define_method, @name, Proc.new {|*args|
not_found = -> { raise "no method #{name.to_sym} found with #{args.count} args" }
@fns.detect(not_found) { |fn| fn.fetch(:arity) == args.count }.fetch(:proc).call(*args)
})
end