Skip to content

Instantly share code, notes, and snippets.

class Animal < ActiveRecord::Base
end
# block: 0.052654
# yield: 0.015362
def with_block(&block)
block.call
end
def with_yield
yield
end
# Erector Page base class.
#
# Allows for accumulation of script and style tags (see example below) with either
# external or inline content. External references are 'uniq'd, so it's a good idea to declare
# a js script in all widgets that use it, so you don't accidentally lose the script if you remove
# the one widget that happened to declare it.
#
# At minimum, child classes must override #body_content. You can also get a "quick and dirty"
# page by passing a block to Page.new but that doesn't really buy you much.
#
# Ruby implementation of Smalltalk's "if" construction. See also
# http://pozorvlak.livejournal.com/94558.html
#
# Note that since I can't cleanly pass in more than one anonymous block to a
# method in Ruby, I have to implement it as two methods rather than
# Smalltalk's cleaner "ifTrue:ifElse:" single method. This means that in order
# to get the syntax for chaining, I lose Ruby's ability to return a value from
# "if", which is pretty cool in itself.
#
class TrueClass
# likewise require
# by Pat Nakajima
# because I've been drinking more than Jack Barnette
# and
# to annoy John Susser and Aaron Peterson
#
# use to require files relative to the current file:
#
# likewise do
# require "foo" # require current-dir/lib
@alexch
alexch / quiz.rb
Created May 15, 2009 22:19 — forked from ryanb/quiz.rb
# COMMUNITY CHALLENGE
#
# How would you test this Quiz#problem method? Only two rules:
#
# 1. The tests should fail if any part of the application breaks.
# For example: If "gets" is moved before "puts" then the tests should
# fail since that breaks the application.
#
# 2. You cannot change the Quiz class. But you can use whatever framework
# and tools you want for the tests. (RSpec, Cucumber, etc.)
@alexch
alexch / bench.rb
Created May 20, 2009 23:29 — forked from nex3/bench.rb
#!/usr/bin/env ruby
# Alex's changes:
# * erector and erector_mixin
# * each template now includes a loop and a variable (this affects HAML quite a lot)
# * automated comparison and benchmarking
# * make sure variable is truly being interpolated
# # of iterations = 10000
# user system total real
# from: http://pivotallabs.com/users/alex/blog/articles/865-monkey-patch-du-jour
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
def log_info(sql, name, ms)
if @logger && @logger.debug?
@logger.debug("#{sql}, #{name}, #{ms}")
c = caller.detect{|line| line !~ /(activerecord|active_support|__DELEGATION__|\/lib\/|\/vendor\/plugins|\/vendor\/gems)/i}
c.gsub!("#{File.expand_path(File.dirname(RAILS_ROOT))}/", '') if defined?(RAILS_ROOT)
name = '%s (%.1fms) %s' % [name || 'SQL', ms, c]
1) Failure:
test_logout(AppTest) [/Users/chaffee/dev/sessiontest/gist-131374/login_app_test.rb:33]:
<"logged out barack"> expected but was
<"logged out ">.
2) Failure:
test_main_page(AppTest) [/Users/chaffee/dev/sessiontest/gist-131374/login_app_test.rb:48]:
please log in: <form action='/login' method='get'><input name='username'><input type='submit'></form>.
<nil> is not true.
# A group of actions and related helper methods dealing with login/logout.
# These are loaded from my main Sinatra app via "register Actions::Login".
# I put this file in a subdirectory called "actions".
module Actions
module Login
module Helpers
def logged_in?
if session[:user_id]
true