Today I learned that you can pass a block to, like so:

klass = do
def self.speak

>> klass.speak
=> "hi!

This is a handy for creating one-off classes; the kind you'll throw away quickly.

It's likely that you'd do this during tests. Here's a test in active_support that uses this technique:

class ClassAttributeAccessorTest < Test::Unit::TestCase
def setup
@class = do
cattr_accessor :foo
cattr_accessor :bar, :instance_writer => false
cattr_reader :shaq, :instance_reader => false

# Now grab an instance of our new class
@object =

# Elided: tests that make sure the cattr_accessor method behaves


Another thing I picked up at the same time: you can pass a constant to and it will set that as the superclass:

>> klass =
=> #
>> klass.superclass
=> String

Here's another test from Rails. This one makes use of both tricks I just showed you:

test "configuration is crystalizeable" do
parent = { include ActiveSupport::Configurable }
child =

# ...