Skip to content

Instantly share code, notes, and snippets.

@travisofthenorth
travisofthenorth / LRUCache.rb
Last active August 29, 2015 14:19
A quick-and-dirty LRU Cache in ruby
class ListEntry
attr_accessor :next, :prev, :data
def initialize(data)
@next = nil
@prev = nil
@data = data
end
end
class List
@travisofthenorth
travisofthenorth / sort.rb
Last active November 14, 2016 17:05
Ruby merge functions
module Sort
def mergesort!(a)
a.replace(mergesort(a))
end
def mergesort(a)
return a if a.length <= 1
halfway = a.length / 2
left, right = a[0..halfway - 1], a[halfway..a.length]
@travisofthenorth
travisofthenorth / graph.rb
Last active October 11, 2015 16:45
Graph implementation in Ruby
class Graph
def initialize
@nodes = []
end
def add_node(name)
fail 'Node already exists!' if find_node(name)
nodes << Node.new(name)
nodes.last
end
@travisofthenorth
travisofthenorth / ripples.js
Last active March 29, 2016 01:10
Grayscale ripple sketch
/**
* Original original code (Java) by Neil Wallis
* @link http://www.neilwallis.com/java/water.html
*
* Original JS code by Sergey Chikuyonok ([email protected])
* http://chikuyonok.ru
*/
(function(){
var canvas = document.getElementById('c'),
/** @type {CanvasRenderingContext2D} */
# Print all combinations of balanced parentheses of length n
def recurse(str:, open:, closed:, n:)
if open + closed == n - 1
str += ')'
p str
else
if open < n / 2
recurse(str: str + '(', open: open + 1, closed: closed, n: n)
end
@travisofthenorth
travisofthenorth / factory_girl_helper.rb
Last active July 30, 2016 15:29
Override factory girl to enable first_or_create type functionality
module FactoryGirlHelper
BLACKLIST = [:user]
def first(args)
name = args.first
klass = name.to_s.camelize.constantize
conditions = args.last
if conditions.present? && conditions.is_a?(Hash)
@travisofthenorth
travisofthenorth / jailed_console.rb
Last active June 3, 2016 23:44
Module to sandbox the rails console more
module JailedConsole
extend ActiveSupport::Concern
included do
require 'colorize'
def sandbox_console
require 'webmock'
require 'sidekiq/testing' if defined?(Sidekiq)
end
@travisofthenorth
travisofthenorth / dankify_specs.sh
Last active September 28, 2017 20:04
If you're using FactoryGirl, replace all occurrences of 'create' with 'build' and 'git add' the specs that still pass.
#!/bin/bash
find spec -iname "*spec.rb" -exec sed -i.bak 's/create/build/g' {} \;
find spec -iname "*spec.rb" -print0 | while IFS= read -r -d $'\0' spec; do
spring rspec $spec --fail-fast
if [[ $? -gt 0 ]]; then
echo "$spec did not pass"
else
echo "$spec passed!!!"

Keybase proof

I hereby claim:

  • I am travisofthenorth on github.
  • I am travishunter (https://keybase.io/travishunter) on keybase.
  • I have a public key whose fingerprint is 3D8C 4969 767F 1A5F E4B9 714C B781 0CC4 B1C2 283F

To claim this, I am signing this object:

@travisofthenorth
travisofthenorth / matrix_binary_search.rb
Last active November 17, 2016 17:15
Matrix binary search
class Matrix
Coordinate = Struct.new(:x, :y)
def initialize(grid)
@grid = grid
end
def binary_search(n)
row = find_row(n, 0, grid.count - 1)
return nil unless row