Skip to content

Instantly share code, notes, and snippets.

View gbuesing's full-sized avatar

Geoff Buesing gbuesing

View GitHub Profile
# Middleware to tap into request so that you can provide an alternate response depending upon request conditions.
#
# Supplied block should return a Rack-compatible response, or nil/false to pass on handling of the request.
#
# Example:
#
# use Rack::Tap do |env|
# if Rack::Request.new(env).host == 'admin.myapp.com'
# [301, {'Location' => 'http://www.myapp.com/admin'}, []]
# end
@gbuesing
gbuesing / ad_hoc.rb
Created June 20, 2013 04:53
Rack::AdHoc
# Allows you to write one-off middleware directly in config.ru. Good for prototyping.
#
# Example:
#
# use Rack::AdHoc do |app, env|
# status, headers, body = app.call(env)
# headers['X-Foo'] = 'bar'
# [status, headers, body]
# end
#

Declared ETags, together with Russian Doll caching, can be used to automatically mix your template and asset versions into the ETags set in your controllers. This avoids the need to blow all browser caches on each deploy and neatly contains the scope of "freshness fallout" when you tweak a view.

To include the template's version in the ETag:

  # Incorporate the cache version for this action into our ETag.
  # This allows template changes to bubble up into HTTP cache
  # freshness and bust browser caches when we make changes.
  etag do
    begin
@gbuesing
gbuesing / btclearcache.sh
Created September 20, 2014 03:39
Clear the Bluetooth attributes cache on Mac OS X
#!/bin/sh
# Clear the Bluetooth attributes cache on Mac OS X
# From http://expertsoverflow.com/questions/23549859/clearing-corebluetooth-gatt-cache-without-removing-bond
sudo defaults write /Library/Preferences/com.apple.Bluetooth CoreBluetoothCache -dict
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.blued.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.blued.plist
@gbuesing
gbuesing / temp_sensor.ino
Last active August 29, 2015 14:06
Arduino BLE temperature sensor w/ nRF8001
// BLEPeripheral: https://github.com/sandeepmistry/arduino-BLEPeripheral
// TimerOne: https://code.google.com/p/arduino-timerone/
#include <TimerOne.h>
#include <SPI.h>
#include <BLEPeripheral.h>
// Pins used for Adafruit nrf8001 breakout
// See https://learn.adafruit.com/getting-started-with-the-nrf8001-bluefruit-le-breakout/hooking-everything-up
#define BLE_REQ 10
#define BLE_RDY 2
@gbuesing
gbuesing / ml-ruby.md
Last active February 28, 2024 15:13
Resources for Machine Learning in Ruby

UPDATE a fork of this gist has been used as a starting point for a community-maintained "awesome" list: machine-learning-with-ruby Please look here for the most up-to-date info!

Resources for Machine Learning in Ruby

Gems

@gbuesing
gbuesing / pca.rb
Created March 30, 2015 01:53
Principal Component Analysis in Ruby
# Ruby implementation of PCA as described in:
#
# A tutorial on Principal Components Analysis
# Lindsay I Smith February 26, 2002
# http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf
#
# Install:
#
# brew install gsl
# brew install gnuplot --with-x11
@gbuesing
gbuesing / linear_regressor.rb
Last active August 29, 2015 14:18
Multivariate linear regression in Ruby - adapted from example from Andrew Ng's Machine Learning Coursera class
require 'narray' # gem install narray
class LinearRegressor
attr_reader :theta, :mean, :std, :cost_history
def initialize opts = {}
@alpha = opts[:alpha] || 0.01
@iterations = opts[:iterations] || 400
end
@gbuesing
gbuesing / nmatrix_ext.rb
Last active August 28, 2015 18:49
NMatrix non-contiguous row/col selection extension
require 'nmatrix/nmatrix'
NMatrix.class_eval do
def row(row_number, get_by = :copy)
if row_numbers = get_row_or_col_index_array(row_number)
out = NMatrix.new [row_numbers.length, cols], default_value, dtype: dtype, stype: stype
row_numbers.each_with_index do |rownum, i|
out[i, 0..-1] = self[rownum, 0..-1]
@gbuesing
gbuesing / nmatrix_standardize_columns.rb
Created September 5, 2015 19:18
NMatrix standardize_columns extension
require 'nmatrix/nmatrix'
class NMatrix
def standardize_columns!
cols.times do |i|
col = self[0..-1,i]
self[0..-1,i] = (col - col.mean[0]) / col.std[0]
end
self
end