Skip to content

Instantly share code, notes, and snippets.

@dariocravero
dariocravero / Gemfile
Created September 5, 2012 18:48
Minimal sinatra app with puma's config.
source "https://rubygems.org"
gem 'puma'
gem 'sinatra'
@robertjwhitney
robertjwhitney / film.rb
Created April 22, 2012 23:45
Conditionally display an image instead of the file input in an ActiveAdmin has_many association.
# active_admin and formtastic seem kinda picky about displaying both the
# image and the file input together here... in fact, it seems like this
# is the ONLY way to do this? Adding anything else after the image_tag squashes it.
ActiveAdmin.register Film do
form do |f|
f.inputs "Film" do
f.input :title
end
f.has_many :stills do |film_still_form|
@jrus
jrus / gist:2145082
Created March 21, 2012 06:08
sort a javascript array using a decorate-sort-undecorate pattern; especially useful if the sort comparison is expensive to compute
# Accepts a list and a key function to apply to each item,
# and then uses a decorate-sort-undecorate pattern to actually
# do the sorting. Returns a new list. Keeps track of the index
# to make sure the sort is stable.
sort_by = (list, key) ->
cmp = ({key: k_a, index: i_a}, {key: k_b, index: i_b}) ->
if k_a == k_b then i_a - i_b else if k_a < k_b then -1 else 1
decorated = ({item, index, key: key item} for item, index in list)
item for {item} in decorated.sort cmp
@andrewheins
andrewheins / qs_to_hash.rb
Created December 7, 2011 17:29
QueryString to Hash in Ruby
# Today's little useless tidbit converts a url's query string into a hash
# set of k/v pairs in a way that merges duplicates and massages empty
# querystring values
def qs_to_hash(querystring)
keyvals = query.split('&').inject({}) do |result, q|
k,v = q.split('=')
if !v.nil?
result.merge({k => v})
elsif !result.key?(k)
require 'socket'
socket = TCPSocket.new('localhost', 7894)
socket.puts "'B'*50024"
all_data = []
while partial_data = socket.read(2024)
puts partial_data
puts "-------------------------"
all_data << partial_data