DEV Community

Fran C. for Factorial

Posted on

A trick with Ruby anonymous classes

Since everything in Ruby is an object it is only normal that classes are objects too. Which means they just have regular constructors with initializers, as any other object would do. do
  def hello
    puts "Hello world!"

# prints -> Hello world!
Enter fullscreen mode Exit fullscreen mode

What can I use it for?

Anonymous classes are an amazing candidate for testing mixings.

# callable_test.rb
# run it with:
# ruby callable_test.rb

module Callable
  def call(*args)

require "minitest/autorun"

class TestCallable < Minitest::Test
  def setup
    @klass = do
      extend Callable

      def initialize(arg1, arg2)
        @arg1 = arg1
        @arg2 = arg2

      def call
        [@arg1, @arg2]

  def test_same_return
    assert_equal, 2).call,, 2)

# Run options: --seed 3295
# # Running:
# .
# Finished in 0.000605s, 1651.9615 runs/s, 1651.9615 assertions/s.
# 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
Enter fullscreen mode Exit fullscreen mode

There's a caveat though... You need to be aware that anonymous classes have no name (they are anonymous after all ;) ) so any call to will return nil.

Classes only acquire a name when you assign them to a constant. That is:

klazz =
# => nil

MyClass = klazz
# => "MyClass"
Enter fullscreen mode Exit fullscreen mode

So, if you need to test things which depend on the class name you can't use anonymous classes, although they still have a benefit over regular declarations on tests, which is that they can be assigned to variables, while regular class...end declarations can't.

my_class = class MyClass; end
# => nil

other_class = OtherClass =
# => OtherClass
Enter fullscreen mode Exit fullscreen mode

So, if you need a class with a name on a test you can still use this, only remember to remove the constant on your test's teardown.

Object.send(:remove_const, :MyClass)
Enter fullscreen mode Exit fullscreen mode

If you're on a Rails app and use RSpec, it even has a helper to test things with anonymous controllers:, which is especially useful to test things like login/logout flows independently.

Top comments (0)