Skip to content

Instantly share code, notes, and snippets.

class MyTestController < ApplicationController
def test_stuff
# bla
end
end
# Add to config/routes.rb...
#
# if Rails.env.test?
# map.connect '/:controller/:action/:id'
@jonleighton
jonleighton / output.txt
Created January 24, 2011 14:02
Rack test problem
$ ruby rack_proxy.rb
Loaded suite rack_proxy
Started
E
Finished in 0.004785 seconds.
1) Error:
test_foo(AppTest):
NoMethodError: undefined method `closed?' for nil:NilClass
/home/turnip/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/net/http.rb:1069:in `begin_transport'
@jonleighton
jonleighton / base64ArrayBuffer.js
Last active April 19, 2024 21:54
Encode an ArrayBuffer as a base64 string
// Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then
// use window.btoa' step. According to my tests, this appears to be a faster approach:
// http://jsperf.com/encoding-xhr-image-data/5
/*
MIT LICENSE
Copyright 2011 Jon Leighton
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
From 42a20533837e925030b07132eaf0a2a6534ad235 Mon Sep 17 00:00:00 2001
From: Jon Leighton <j@jonathanleighton.com>
Date: Fri, 13 May 2011 20:03:57 +0100
Subject: [PATCH] Unfailing test case
---
.../has_many_through_associations_test.rb | 21 ++++++++++++++++++++
activerecord/test/schema/schema.rb | 11 ++++++++++
2 files changed, 32 insertions(+), 0 deletions(-)
@jonleighton
jonleighton / public_send_benchmark.rb
Created July 18, 2011 14:34
A benchmark of the speed of two options for backporting Ruby 1.9's public_send method to 1.8.7. The first one wins by far.
require 'benchmark'
class Object
def public_send_public_method_defined(method, *args, &block)
singleton_class = (class << self; self; end)
if singleton_class.public_method_defined?(method)
send(method, *args, &block)
else
raise # this doesn't matter, as it's not the path we're benchmarking
@jonleighton
jonleighton / results.txt
Created July 18, 2011 15:04
Proving to myself that it really is faster to avoid using send...
# 1.9.2
Rehearsal -------------------------------------------------------
no-send 1.950000 0.000000 1.950000 ( 1.961587)
symbol-send 2.190000 0.000000 2.190000 ( 2.192600)
string-send 4.790000 0.000000 4.790000 ( 4.812341)
---------------------------------------------- total: 8.930000sec
user system total real
no-send 2.330000 0.000000 2.330000 ( 2.332294)
require 'active_record'
require 'logger'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:user => 'root',
:database => 'test'
)
# encoding: utf-8
def λ(&block)
block
end
I = λ { |x| x }
I.(3) # => 3
##### Pointless: #####
# Bad:
Foo.new.tap do |foo|
foo.bar = "<3"
end
# Good:
foo = Foo.new
foo.bar = "<3"
class A
def foo
"<3"
end
end
class B < A
def foo
# super will still call A#foo, even after the method is aliased
super * 2