Skip to content

Instantly share code, notes, and snippets.

View jsanders's full-sized avatar

James Sanders jsanders

View GitHub Profile
@jsanders
jsanders / echod.go
Created July 23, 2012 16:18
Simple echo server in Go
package main
import (
"fmt"
"net"
"os"
"bufio"
)
func printBanner() {
# in .bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# in .bashrc
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
@jsanders
jsanders / shared_examples_for_active_model.rb
Created August 28, 2012 17:04
Rspec shared examples for ActiveModel lint
shared_examples "ActiveModel" do
class BeBoolean
def matches?(it)
@it = it
it.kind_of?(TrueClass) || it.kind_of?(FalseClass)
end
def failure_message
"expected #{@it} to be either true or false"
end
@jsanders
jsanders / spies_on_class_method_spec.rb
Created August 30, 2012 15:17
Test case for gimme class method spies
require 'spec_helper'
module Gimme
class ChairFactory
def self.build
raise RuntimeError.new "unimplemented feature"
end
def self.destroy
@jsanders
jsanders / rescuable_exception.rb
Created October 31, 2012 17:16
Rescuable Exception
# Taken from a comment on http://www.mikeperham.com/2012/03/03/the-perils-of-rescue-exception/
module RescuableException
def self.===(exception)
case exception
# Catch when the user hits ^C (Interrupt < SignalException), and assume
# that they just wanted to stop the in-progress command (just like bash etc.)
when Interrupt
true
@jsanders
jsanders / Gemfile
Last active December 12, 2015 05:59
Resque memory test
source 'https://rubygems.org'
gem 'json'
gem 'resque', '1.19.0'
@jsanders
jsanders / Gemfile
Last active December 12, 2015 06:19
Sidekiq profiling
source 'https://rubygems.org'
gem 'sidekiq'
@jsanders
jsanders / subsets.clj
Created May 7, 2013 21:24
Clojure subsets
(use 'clojure.contrib.combinatorics)
(def small-numbers [ 1 2 3 4 6 ])
(def large-numbers [ 3 4 9 14 15 19 28 37 47 50 54 56 59 61 70 73 78 81 92 95 97 99 ])
(defn largest-sum-of-rest?
[ col ]
(let [ [ largest & others ] (reverse (sort col)) ]
(= largest (reduce + others))))
@jsanders
jsanders / gist:5551350
Created May 9, 2013 23:19
Go get issue
$ GOPATH=$HOME/go go get github.com/burke/zeus/go/cmd/zeus
../../../go/src/github.com/burke/zeus/go/cmd/zeus/zeus.go:14:2: no Go source files in /Users/james/go/src/github.com/burke/zeus/go/zeusversion
@jsanders
jsanders / aes_ctr_cbc.rb
Last active October 8, 2021 10:45
Implementation of AES with counter (CTR) and cipher-block-chaining (CBC) modes. Based on spec at http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf. ** Disclaimer: For educational purposes only. Obviously, nobody should ever use a hacky un-vetted non-standard (not to mention, non-optimized) implementation of crypto like this **
require File.expand_path('../../utilities', __FILE__)
require 'openssl'
# Set to true to see debug output
DEBUG = false
def debug_puts(s=nil); puts(s) if DEBUG; end
def debug_print(s=nil); print(s) if DEBUG; end
# Encrypt data using given `mode`, `key_b`, `iv_b` and `data_b`, all as byte arrays
# Only uses padding in CBC mode