Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today. with Superclass

Today I encountered the following syntax (observed in Ruby 2.7):

SpecialError =

What’s going on here?

According to the Class docs, takes a superclass as an argument, creating an anonymous unnamed class. So this could be written as:

class SpecialError < StandardError

We’d lose the anonymity of the SpecialError class, but in the first example, we are assigning it to a Pascal-cased variable name anyway.

I think the first syntax would make sense when the code looks just like this example: a simple class that inherits some behavior, where the name is needed only in the scope where the class is declared.

Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.