Skip to content

Instantly share code, notes, and snippets.

@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)
# 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 / 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} */
@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 / 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 / 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