Today I encountered the following syntax (observed in Ruby 2.7):
SpecialError = Class.new(StandardError)
What’s going on here?
According to the Class docs,
Class.new takes a superclass as an argument, creating an anonymous unnamed class. So this could be written as:
class SpecialError < StandardError end
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.Tweet