Skip to content

Instantly share code, notes, and snippets.

View kenkeiter's full-sized avatar
🦊

Ken Keiter kenkeiter

🦊
View GitHub Profile
@kenkeiter
kenkeiter / homebrew_yosemite_valgrind.txt
Created October 20, 2014 00:33
Homebrew output when building valgrind on Yosemite.
~ → brew install valgrind
valgrind: OS X Mavericks or older is required for stable.
Use `brew install devel or --HEAD` for newer.
Error: An unsatisfied requirement failed this build.
~ → brew edit valgrind
~ → brew install valgrind
==> Downloading http://valgrind.org/downloads/valgrind-3.10.0.tar.bz2
######################################################################## 100.0%
==> Downloading https://gist.githubusercontent.com/jacknagel/369bedc191e0a0795358/raw/a71e6c0fdcb786fdfde2fc33d71d555b18
######################################################################## 100.0%
@kenkeiter
kenkeiter / go_marshaling_thoughts.md
Created October 5, 2014 16:41
Quick thoughts on separation of representation from implementation in Go serialization.

If your application demands marshaling to JSON or another serialiazation format using Go, it can be tempting to design your structures to be cleanly serializable – often to the detriment of the implementation.

You might find yourself exporting variables you shouldn't be exporting, or creating elaborate ways to prevent mutability of attributes you care about.

If you find yourself doing this, stop, and consider the separation of concerns. JSON is a representation of your structure, and should not be directly tied to its underlying implementation, unless it's convenient. Although it's a bit more expensive, consider creating custom marshaling functions and generating one-off structs with the exact fields you need to decouple the implementation from representation.

In the following example, you don't want to export the Value field from the Counter struct, because it exposes your value to potentially unsafe operations (two incrementations at the same time, for example):

type Counter struct {
@kenkeiter
kenkeiter / rescue_time.rb
Created August 25, 2014 22:37
Get your RescueTime productivity using Ruby and the RescueTime API.
require 'rest_client'
require 'json'
class RescueTimeProfile
API_BASE_URL = 'https://www.rescuetime.com/anapi'
TIME_SCORE_SCALE = {
2 => 1,
1 => 0.75,
0 => 0.50,
@kenkeiter
kenkeiter / rescue_nimbus.rb
Created August 20, 2014 14:52
Display RescueTime productivity on the Quirky Nimbus
require 'rest_client'
require 'json'
require 'pp'
class RescueTimeProfile
API_BASE_URL = 'https://www.rescuetime.com/anapi'
TIME_SCORE_SCALE = {
2 => 1,
@kenkeiter
kenkeiter / rabl_patch.rb
Created July 17, 2014 02:18
If you're using Rabl with DataMapper, lazy collection queries won't be evaluated correctly, and you'll end up SELECTing each record from the database. This patch fixes that issue (albeit, in an ugly way).
module Rabl
class Engine
def collection(data, options={})
self.object(data_object(data))
end
end
module Helpers
def data_name(data_token)
@kenkeiter
kenkeiter / pyvirtualenv.rb
Created July 6, 2014 01:58
A quick Ruby wrapper for manipulating Python's virtual environments.
require 'open3'
class VirtualEnvError < Exception; end
class PythonVirtualEnv
def self.exists?(path)
File.directory?(path) and File.exists?(File.join(path, 'bin/activate'))
end
@kenkeiter
kenkeiter / config.ru
Last active December 17, 2015 06:18
If you've tried to mount RESTful resources at '/' with Padrino, you've probably run into issues with its router, and had to :map each action (or develop some idiom to generate the mappings for you). Padrino's router is pretty opinionated; when you're using named controllers, you'll want to make use of `:parent` directives -- which you can't, if …
# You'll need to add 'rack-rewrite' to your Gemfile.
require 'rack/rewrite'
require File.expand_path("../config/boot.rb", __FILE__)
# lazy load the application, instantiating routes.
application = Padrino.application
# Determine which routes NOT to redirect to your root resource.
reserved_routes = YourProject::App.routes.map{|r|
r.path_for_generation.scan /[a-zA-Z0-9]+/i
@kenkeiter
kenkeiter / Nested resources.coffee
Created March 15, 2013 20:49
Coffee script nested resources example (may or may not be working -- kind of pseudo-code).
class User extends Spine.Model
url: ->
'/' + @username
collections: ->
unless @collection_klass?
@collection_klass = _.clone(Collection)
@collection_klass.parent = this
@collection_klass
@kenkeiter
kenkeiter / grapeish.rb
Last active December 10, 2015 02:38
Fix grape's incredible brokenness.
require 'sinatra/base'
require 'sinatra/namespace'
# You may need multi_json
class API < Sinatra::Base
# Respond to all errors with JSON
error do
content_type :json
e = env['sinatra.error']
@kenkeiter
kenkeiter / guards.rb
Last active August 3, 2018 19:34
Guard-like things in Ruby
class Proc
def call_with_vars(vars, *args)
Struct.new(*vars.keys).new(*vars.values).instance_exec(*args, &self)
end
end
module Touchy
class GuardError < Exception; end
class GuardSignatureError < GuardError; end