Skip to content

Instantly share code, notes, and snippets.

sleep 10 && say "meow meow meow? miaau meeaauuu miiieeaauu!" && open romyromy.com
@lfborjas
lfborjas / collection_contract.rb
Created August 3, 2012 03:30
'cause I want to receive one or the other and actually coerce the value
module CollectionContract
extend ActiveSupport::Concern
module ClassMethods
#I want to be able to say what I expect and do the appropriate casting if needed
#in my specific use-case, these are the only two possible arg types
def collection_method(name, opts = nil, &definition)
opts ||= {}
define_method name do |*args|
rules = {
@lfborjas
lfborjas / graph.java
Created August 1, 2012 04:31
A simple adjacency list implementation in OOP and not-so-OOP
/**I actually needed a typed language to make this clear, but fret not, there's a ruby example below!*/
public class Graph{
static class Vertex{
/** Every vertex is aware of its immediate adjacent neighbors*/
private List<Vertex> adjacent_vertices;
public void add_adjacent(Vertex neighbor){
adjacent_vertices.append(neighbor);
}
}
@lfborjas
lfborjas / sartre.rb
Created July 31, 2012 14:05
rescuing our vars from existential angst
puts "we can an expression in blocks hold"
x = begin; 'a'; end
y = begin; b = 42; end
puts "but identity crises, sometimes unfold"
z = begin; c; end
puts "we can rescue our little vars, though"
w = begin
o
require 'sinatra'
before do
content_type :txt
@defeat = { rock: :scissors, paper: :rock, scissors: :paper }
@throws = @defeat.keys
end
get '/throw/:type' do
player_throw = params[:type].to_sym
@lfborjas
lfborjas / huffman.rb
Created July 24, 2012 20:35
Compresses a string using the literal approach to huffman encoding
def freq_table(str)
str.each_char.inject(Hash.new(0)) do |table, char|
table[char] += 1
table
end.sort_by(&:last).reverse
end
def build_tree(freq_table)
until freq_table.size == 1
less_frequent = 2.times.map{ freq_table.pop }
@lfborjas
lfborjas / huffman.py
Created July 24, 2012 02:12 — forked from ryanwitt/huffman.py
silly huffman coding example
import subprocess
import sys
from subprocess import PIPE, Popen
import os
__all__ = (
'build',
'endode',
'decode',
)
@lfborjas
lfborjas / gist:3158818
Created July 22, 2012 07:55 — forked from dhh/gist:1014971
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@lfborjas
lfborjas / gist:2951234
Created June 18, 2012 22:46
make a decent diff for porting stuff
git diff master porting_ui --stat=120,80 --diff-filter=DM