Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
jnunemaker / null_object.rb
Created April 15, 2012 13:38
basic null object
# from http://avdi.org/talks/confident-code-rubymidwest-2011/confident-code.html
class NullObject
def method_missing(*)
self
end
def nil?
true
end
@jnunemaker
jnunemaker / fetch_as_assertion.rb
Created April 15, 2012 13:38
fetch as an assertion
# from http://avdi.org/talks/confident-code-rubymidwest-2011/confident-code.html
opt = {}.fetch(:required_option)
# IndexError: key not found
opt = {}.fetch(:required_option) do
raise ArgumentError, "Missing option!"
end
# ArgumentError: Missing option!
@jnunemaker
jnunemaker / two_dimentional_list.io
Created March 16, 2012 15:02
Two dimensional list prototype in Io
TwoDimensionalList := Object clone
TwoDimensionalList list := List clone
TwoDimensionalList dim := method(x, y,
for(i, 1, y,
self list push(List clone setSize(x))
self
)
)
@jnunemaker
jnunemaker / heroku_create_app.rb
Created January 30, 2012 22:08
Simple script that shows how to create heroku app without command line client
require 'rubygems'
require 'highline/import'
require 'heroku'
require 'pp'
email = ask('Email: ')
password = ask('Password: ') { |q| q.echo = false }
client = Heroku::Client.new(email, password)
class UserPresenterTest < Test::Unit::TestCase
def test_as_json
user = Factory(:user, {
:first_name => 'John',
:last_name => 'Nunemaker',
:email => 'john@doe.com',
})
json = UserPresenter.new(user).as_json
urls = json['urls']
@jnunemaker
jnunemaker / fu.md
Created July 12, 2011 23:20
FUN BUG?!

Posted to the REE Google Group as well: https://groups.google.com/forum/#!topic/emm-ruby/EBkMfFpk40A

A week back I innocently updated Sinatra on an app which in turn updated Rack. Since then, I've had crazy hung passenger processes that just gobble up CPU like it is going out of style.

After spending a few days trying everything I knew to to fix it, today I got help from a friend (Eric Lindvall) and dug in with strace, rbtrace, gdb, and gdb.rb and found the issue. Rack 1.3.0 tests a regex against a URL and it causes things to hang.

@jnunemaker
jnunemaker / gist:841656
Created February 24, 2011 02:38
all time views across system for http://gaug.es
# How we store all time views across the entire system for http://gauge.es.
# All in one document that gets incremented using MongoDB $inc modifier in
# every track. The $inc increments t, year.t, year.month.t, year.month.day.t
# so we get to the day numbers. These are in EST, as we are in EST and these
# stats are just for us. :) Nothing amazing, but thought I would share.
#
# >> pp View.all_time
{
"_id" => "all_time",
"t" => 502352,
@jnunemaker
jnunemaker / gist:833256
Created February 18, 2011 04:38
Why You Should Never Use An ORM
Why You Should Never Use An ORM
Having built two object mappers in Ruby (MongoMapper and ToyStore), I would like to
throw out a crazy thought. What if, on your next project, you ditch the ORM.
No ActiveRecord. No DataMapper. No anything. Just you and a lower level driver, whispering
sweet nothings into Ruby classes and modules. Could you? Would you? DARE you?
We Rubyists are all about aesthetics, right? We love declaring has_many and belongs_to!
Scope this! Boom! Instant magic happens behind the scenes. Fancy proxies are assembled,
@jnunemaker
jnunemaker / zlib_mongo_grid.rb
Created December 27, 2010 15:39
benchmarks of put/get/read with/without zlib compression
require 'pp'
require 'zlib'
require 'benchmark'
require 'rubygems'
require 'active_support/gzip'
require 'mongo_mapper'
MongoMapper.database = 'testing'
MongoMapper.database.collections.each(&:remove)
# gem install sunspot_rails
class Foo
include MongoMapper::Document
include Sunspot::Rails::Searchable
key :title, String
searchable do
text :title
end