Skip to content

Instantly share code, notes, and snippets.

View rainkinz's full-sized avatar
🏠
Working from home

Brendan Grainger rainkinz

🏠
Working from home
View GitHub Profile
@henrik
henrik / hash_deep_diff.rb
Created July 14, 2009 08:38
Recursively diff two Ruby hashes.
# Recursively diff two hashes, showing only the differing values.
# By Henrik Nyh <http://henrik.nyh.se> 2009-07-14 under the MIT license.
#
# Example:
#
# a = {
# "same" => "same",
# "diff" => "a",
# "only a" => "a",
# "nest" => {
@jamiehodge
jamiehodge / gist:1327195
Created October 31, 2011 09:38
Warden and Sinatra example
require 'sinatra/base'
require 'rack/flash'
require 'warden'
require 'slim'
require 'sequel'
require 'sqlite3'
DB = Sequel.sqlite
DB.create_table :users do
primary_key :id
@padde
padde / svg_builder.rb
Created November 2, 2011 12:20
Make SVG with builder
# encoding: utf-8
require 'rubygems'
require 'builder'
svg_doctype = [
:DOCTYPE,
:svg,
:PUBLIC,
"-//W3C//DTD SVG 1.1//EN",
@jeduan
jeduan / reveal_ajax.js
Created February 9, 2012 00:15
Use Zurb reveal with ajax
$('a.reveal').click(function(event) {
event.preventDefault();
var $div = $('<div>').addClass('reveal-modal').appendTo('body'),
$this = $(this);
$.get($this.attr('href'), function(data) {
return $div.empty().html(data).append('<a class="close-reveal-modal">&#215;</a>').reveal();
});
});
@andrey-kazakov
andrey-kazakov / socksify_faraday.rb
Created February 17, 2012 18:27
HTTP over SOCKS support monkey patch for Mechanize, Faraday and it's based clients (OAuth2 like)
# requires socksify gem
require "socksify"
require 'socksify/http'
# use w/ OAuth2 like OAuth2::Client.new(id, secret, connection_opts: { proxy: 'socks://127.0.0.1:9050' })
class Faraday::Adapter::NetHttp
def net_http_class(env)
if proxy = env[:request][:proxy]
if proxy[:uri].scheme == 'socks'
Net::HTTP::SOCKSProxy(proxy[:uri].host, proxy[:uri].port)
@austenito
austenito / integration_spec.rb
Created April 11, 2012 12:50
Integration Testing w/ Sorcery
describe "Shopping Cart Requests" do
let!(:user) { Fabricate(:user) }
before(:each) do
login_user_post("admin", "admin")
end
context "when I visit the shopping cart" do
it " show the logged in users' cart items " do
#Test stuff
@jrochkind
jrochkind / report.markdown
Created December 19, 2012 15:10
Is Set faster than Array? Not neccesarily, depending on size of collection and number of reads vs writes. (MRI) (anything you notice invalid or could be done better about the way these benchmarks are done? Let us know, and give us code and results for the better way :) )
# test_for == N / 5 -- approximate array average case?
{:benchmark_iterations=>10000, :class=>Array, :test_for=>5, :num_rewrites=>1, :col_size=>10, :num_reads=>10}
0.140000 0.000000 0.140000 ( 0.141915)
{:benchmark_iterations=>10000, :class=>Set, :test_for=>5, :num_rewrites=>1, :col_size=>10, :num_reads=>10}
0.190000 0.010000 0.200000 ( 0.198503)
----
{:benchmark_iterations=>10000, :class=>Array, :test_for=>50, :num_rewrites=>1, :col_size=>100, :num_reads=>10}
(ns workbook.dec-tree)
;; An example drawn from:
;; http://www.doc.ic.ac.uk/~sgc/teaching/pre2012/v231/lecture11.html
(defn log2
"log2(x), log2(0) = 0"
[x]
(if (= x 0) 0 (/ (Math/log x) (Math/log 2))))
@jackrusher
jackrusher / decision-tree-to-match.clj
Created February 22, 2013 16:49
Convert a learnt decision tree into a nice, readable core.match program.
;; Converts a learnt decision tree produced by
;; https://gist.github.com/jackrusher/4971531
;; into a core.match program. A nice way to convert a set of data points into a
;; readable set of rules. I haven't looked at the code, but I assume core.match
;; compiles the rules into a decision tree.
(defn tree-to-match-clauses [t p]
(let [children (keys t)]
(if (keyword? (first children))
(conj p (first (vals t)))
@riywo
riywo / gist:5023060
Created February 24, 2013 07:58
Join mp3 files with homebrew
$ brew install -v mp3wrap ffmpeg id3lib
$ mp3wrap tmp.mp3 1.mp3 2.mp3 3.mp3 ....
$ ffmpeg -i tmp_MP3WRAP.mp3 -acodec copy all.mp3 && rm tmp_MP3WRAP.mp3
$ id3cp 1.mp3 all.mp3