Skip to content

Instantly share code, notes, and snippets.

View gbuesing's full-sized avatar

Geoff Buesing gbuesing

View GitHub Profile
@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]

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 / 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 / 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 / ugly couchrest hacks.rb
Created March 20, 2009 22:11 — forked from will/ugly couchrest hacks.rb
Hash hacks so that CouchRest::Document plays nice with Rails routes
# Hacks so that CouchRest::Document, which descends from Hash,
# doesn't appear to Rails routing as a Hash of options
class Hash
def self.===(other)
return false if other.is_a?(CouchRest::Document)
super
end
end
ActionController::Base.class_eval do
protected
# BACKPORT OF CHANGE FROM 3.0 EDGE: http://github.com/rails/rails/commit/256b0ee8e3c1610967dfc89f864e24b98ed3c236
# Returns true or false if a request is verified. Checks:
#
# * is the format restricted? By default, only HTML requests are checked.
# * is it a GET request? Gets should be safe and idempotent
# * Does the form_authenticity_token match the given token value from the params?
def self.verified_request?
!protect_against_forgery? ||
@gbuesing
gbuesing / rack_honeypot.rb
Created April 29, 2009 19:42
Rack::Honeypot
# Returns a blank 200 OK response for any form posts that include a value for the honeypot field
module Rack
class Honeypot
def initialize(app, field_name)
@app = app
@field_name = field_name
end
20_000 times
user system total real
Time.parse: 3.810000 0.020000 3.830000 ( 3.870988)
Time.parse_gm: 0.420000 0.190000 0.610000 ( 0.596905)
Time.parse_mktime: 0.520000 0.180000 0.700000 ( 0.707689)
Time.parse_maketime: 0.700000 0.190000 0.890000 ( 0.899610)
Time.parse_utc: 0.390000 0.180000 0.570000 ( 0.575244)
Time::mktime_with_offset: 0.760000 0.210000 0.970000 ( 0.965841)
require 'date'
class Article < Hash
def initialize(hsh={})
replace(hsh)
end
def ==(other)
if other.is_a?(Date)
self[:date] == other