Skip to content

Instantly share code, notes, and snippets.

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
@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)
@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
@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|
@dariocravero
dariocravero / Gemfile
Created September 5, 2012 18:48
Minimal sinatra app with puma's config.
source "https://rubygems.org"
gem 'puma'
gem 'sinatra'
@davemo
davemo / app.coffee
Last active March 25, 2020 13:25
Got a .coffee file with JSX? Here's how you can transpile to .js with Reacts JSX parsed.
`/** @jsx React.DOM */`
converter = new Showdown.converter
Comment = React.createClass
render: ->
rawMarkup = converter.makeHtml @props.children.toString()
`<div className="comment">
<h2 className="comment">{this.props.author}</h2>
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
require 'rails_helper'
RSpec.describe TodosController, :type => :controller do
describe "GET #index" do
#describe "POST #create" do
#describe "GET #show" do
#describe "PATCH #update" do (or PUT #update)
#describe "DELETE #destroy" do
#describe "GET #new" do
@evincarofautumn
evincarofautumn / git-branch-description.sh
Created October 29, 2014 18:18
Show git branch description
#!/bin/sh
if [ $# -ne 1 ]; then
echo 'Usage: git-branch-description <branch name>' >&2
exit 1
fi
EDITOR='cat' git branch --edit-description "$1" | grep -v '^#'
@chantastic
chantastic / AudioFileStepSelector.coffee
Created January 18, 2015 19:20
My First React Component
# My First React Component
# Added to this Gist for posterity.
# Extracted from https://github.com/ministrycentered/transposr/commit/fa616871914ac16b72d0d1b035a08e01e337bd07
# January 08, 2014
{ div, input } = React.DOM
AudioFileStepSelector = React.createClass
getInitialState: ->
halfSteps: 2
@yesvods
yesvods / gist:51af798dd1e7058625f4
Created August 15, 2015 11:13
Merge Arrays in one with ES6 Array spread
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr1, ...arr2] //arr3 ==> [1,2,3,4,5,6]