The article does not do a great job of explaining why creating new classes dynamically is preferable to just defining the class your system needs.
You are literally imagining a distinction that does not exist in Ruby.
Point = Data.new(:x, :y) do
def len
Math.sqrt(x**2 + y**2)
end
end
For one, Struct has existed since basically the beginning of time and works exactly the same way (sans the immutability and some nice auto-implemented features).Also, if you’re writing a custom data class, you’d need to write code to support immutability which this article’s approach handles
The idea of distinct “concrete” and “dynamic” classes seems to be a product of a wrong mental model of how Ruby is executed.
There is no effectively no difference between these:
class Foo < Bar
attr_accessor :baz
end
Foo = Class.new(Bar) do
define_method(:baz) do
@baz
end
endIf I see a data class instantiation. I immediately have an idea of its scope and what could/couldnt go wrong with it.
Just not in quite the same way.