Skip to content

Instantly share code, notes, and snippets.

View nevans's full-sized avatar

nicholas a. evans nevans

View GitHub Profile
@nevans
nevans / celluloid-retrying_supervisor_proxy.rb
Created September 11, 2012 20:09
celluloid supervisor that attempts to run the called method no matter what
#!/usr/bin/env ruby
# encoding: UTF-8
require "celluloid"
class BustedActor
include Celluloid
include Celluloid::Logger
def works_great; 42 end
def broken; raise "hell" end
end
@nevans
nevans / rails_class_level_memoization.md
Created April 6, 2012 17:26
seen during a code review

The following code came up during a code review:

def self.account_for(email)
  @account_for ||= {}
  return @account_for[email] if @account_for.include?(email)
  @account_for[email] = Account.find(email)
end
@nevans
nevans / bm.rb
Created March 21, 2012 18:00
Why is EM.epoll slowing down my connections in other threads?
#!/usr/bin/env ruby
# copied from https://gist.github.com/939696, and
# edited to add redis (which is where I was experiencing the issue)
require 'rubygems'
require 'net/http'
require 'hiredis'
require "redis/connection/hiredis"
require 'redis'
@nevans
nevans / foo_task.rb
Created March 8, 2012 15:42
good or bad ruby on rails OO?
# I have a new feature that primarily deals with a single class. It's a
# relatively small and self contained feature; I don't expect that other
# features will ever develop dependencies on it, but it will be highly
# dependant on the single class that it deals with. For various reasons, it
# isn't appropriate to make an Observer class. I'd like to keep all of the
# code pertaining to this feature highly cohesive (localized, in one file if
# possible). But it does make some specific demands of the class that it's
# coupled with.
module FooTask
@nevans
nevans / imap_astring.treetop
Created December 12, 2011 18:00
tree top and IMAP astrings
# See http://tools.ietf.org/html/rfc3501#section-9
# INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1 - Formal Syntax
module IMAP
grammar Astring
# astring = 1*ASTRING-CHAR / string
rule astring
ASTRING_CHAR+ / string
end
@nevans
nevans / sugared_resque_job.rb
Created November 29, 2011 20:20
Maybe a good way to make resque job definition just a tad simpler.
module SugaredResqueJob
def self.new(queue, resque=Resque, &perform)
Module.new do
extend self
define_method :queue do queue end
define_method :enqueue do |*args| resque.enqueue(self, *args) end
define_method :perform do |*args| perform.call( *args) end
end
end
end
@nevans
nevans / resque_enqueue.rb
Created November 21, 2011 16:32
resque: queue.enqueue(job_selector, arg1, arg2)
# in response to https://twitter.com/#!/avdi/status/138513455622791168
# Yes, resque's enqueue API is wacky, but in practice it's not a problem,
# because it's trivial to route around.
# This is untested, and skips any resque enqueue hooks you might have setup.
# but those aren't major hurdles to fix.
class ResqueQueueWrapper
def initialize(queue, resque=Resque)
@queue = queue
@nevans
nevans / gist:1314531
Created October 25, 2011 22:20
premature micro-optimizations are the root of all evil

ruby 1.8.7 (REE)

irb(main):031:0> start = Time.now; 10_000_000.times { 'foo' }; puts Time.now - start
=> 2.154165
irb(main):032:0> start = Time.now; 10_000_000.times { "foo" }; puts Time.now - start
=> 2.177029

Remind me again, why do I care about 23 milliseconds for every 10 million string loads? Methinks that just one instance of needing to change single quotes to double quotes so you can do some interpolation that you hadn't originally

@nevans
nevans / method_vs_constant_lookup.rb
Created August 19, 2011 15:50
demonstrating the difference between method and constant lookup
#!/usr/bin/env ruby
module TopLevel
CONST_D = :d_from_top_level
module MiddleLevel
class BottomLevel
CONST_A = :foo
CONST_B = :bar
CONST_C = :c_from_bottom_level
@nevans
nevans / private_module_methods.rb
Created August 19, 2011 15:20
My favorite module methods pattern, and how it works with private
#!/usr/bin/env ruby
module M
extend self
def foo
"hello world"
end
private