Ideally, I would like to see what is inside the super().tap { |o| block super().tap { |o| , because although this seems like bad practice, there may be some kind of interaction required before or after the initialize call.
Without context, it is possible that you are just looking at something that works, but is not considered good practice in Ruby.
However, it is possible that the approach of the individual self.new and initialize methods allows the framework designer to implement the part suitable for the subclass, and still ensure that the configuration required by the structure is complete without a bit of inconvenient documentation that requires the use of super() . It would be a little easier to document a cleaner API if the end user gets the functionality they expect only by subclassing class MyClass < FrameworkClass and without an extra note, for example:
When you implement the initialize subclass, remember to put super at the beginning, otherwise the magic will not work
., personally, I think that the design is doubtful, but I think that at least there will be a clear motivation.
Perhaps there are deeper reasons for the Ruby language to run code in the self.new custom block β for example, it might allow the designer to switch or modify a specific object (even returning an object of another class) before returning it. However, I rarely saw such things in practice, there is almost always some other way to achieve the goals of such code without setting new .
Examples of custom / different Class.new methods raised in the comments:
Struct.new , which may optionally take the class name and return the objects of this dynamically created class.
Table inheritance for ActiveRecord , which allows the end user to load an object of an unknown class from the table and get the right object.
The latter could have been avoided by using another ORM project for inheritance (although all such schemes have pros and cons).
The first (Structs) is the main language, so now it should work like this (although designers could choose a different method name).
Neil slater
source share