Skip to content

Instantly share code, notes, and snippets.

@camertron
camertron / BitInputStream.java
Last active August 6, 2017 17:15
Java classes for reading and writing streams of bits.
import java.io.IOException;
import java.io.InputStream;
/**
* An input stream capable of reading individual bits from an underlying input stream.
*
* EN 605.421 Foundations of Algorithms
* Johns Hopkins Engineering for Professionals
* Summer 2017
*
@camertron
camertron / asset_preloader.rb
Created June 10, 2017 04:46
Preload assets to make rails dev faster
require 'parallel'
require 'thread'
# The asset preloader is designed to precompute and cache all precompilable
# assets in parallel to avoid doing it in serial on the first request. As of
# Sprockets 3, all assets on the precompile list (i.e. config.assets.precompile)
# are compiled on the first request whether the current page has asked for them
# or not. Obviously such behavior can mean a very slow initial request (we were
# seeing load times on the order of 10-11 minutes). By preloading, or warming the
# sprockets cache, initial page load times can be reduced to ~15 seconds (with
@camertron
camertron / upload_master_resources.rb
Created February 23, 2017 18:49
Txgh upload script
#! /usr/bin/env ruby
ENV['TXGH_CONFIG'] = "file://#{File.expand_path('../../config/txgh-config.yml', __FILE__)}"
require 'bundler'
Bundler.setup
require 'txgh'
config = begin
@camertron
camertron / validate.rb
Created February 23, 2017 18:48
Txgh validation script
#! /usr/bin/env ruby
ENV['TXGH_CONFIG'] = "file://#{File.expand_path('../../config/txgh-config.yml', __FILE__)}"
require 'bundler'
Bundler.setup
require 'txgh'
config = begin
@camertron
camertron / asset_fingerprinter.rb
Last active February 9, 2017 22:28
Fast Sprockets
require 'digest/md5'
# The AssetFingerprinter calculates a digest hash of all the app's assets. It is
# used to avoid precompiling assets if none of them have changed since the last
# precompilation. This is done by generating a list of all assets from the list
# of configured asset paths and throwing all their contents through a digest
# algorithm. The result is a unique asset "fingerprint" that can be compared to
# other fingerprints to determine if any assets have changed.
#
# The fingerprinter also allows adding any additional files that should be used
@camertron
camertron / football.rb
Created December 21, 2016 07:31
Given the number of points earned in a football game, determine all the possible scoring sequences
# name: human-readable name
# point_value: number of points earned
# priority: how likely the score is to actually happen
ScoreType = Struct.new(:name, :point_value, :priority)
SCORE_TYPES = [
ScoreType.new('safety', 2, 5),
ScoreType.new('field goal', 3, 2),
ScoreType.new('touchdown', 6, 3),
ScoreType.new('touchdown with extra point', 7, 1),
@camertron
camertron / envize.rb
Created November 14, 2016 19:51
Move config from a .yml file into the .env
require 'yaml'
require 'optparse'
require 'set'
$options = { keys: [] }
$executable_name = 'envize.rb'
OptionParser.new do |opts|
opts.banner = "Usage: #{$executable_name} [options]"
@camertron
camertron / ruby_learning_5_13_2015.md
Last active August 29, 2015 14:21
Learning team bi-weekly code snippet for 5/13/2015

Three Useful Ruby Methods you Didn't Know Existed, and What to do With Them Now You've Wised Up

Yep, it's time for some cool Ruby trickery.

String#force_encoding

At their core, Ruby strings are just sequences of bytes. How you or your computer chooses interpret and display those bytes can mean the difference between legible text and complete garbage. Consider UTF-8 text for example. We now have great Unicode support in Ruby (as of 1.9) via the String#encode method and its friends, but again, strings are really just bytes.

Telling Ruby your string is encoded in UTF-8 tells it how to print your string on the screen or in your web app. For example, let's say you have a string that contains Japanese text (says 'arigato') "ありがと". Furthermore, let's pretend Ruby thinks the string is encoded in ASCII, which doesn't support Japanese characters. If you tried to print your string in the terminal, you might see something like this: "\xE3\x81\x82\xE3\x82\x8A\xE3\x81\x8C\xE3\x81\xA8". What you're seeing

@camertron
camertron / file_scope.rb
Last active August 29, 2015 14:19
Provides a way to perform Ruby `require` statements at the file (really the block) level
require 'find'
def provide(*requires, &block)
modules = requires.map do |req|
Module.new do
file_fragment = "#{req}.rb"
file = Find.find(*($: + ['.'])) do |f|
break f if f =~ /#{file_fragment}\z/
end