I might go a step further and suggest people overuse Struct to avoid writing attr_accessor . It’s understandable since Rubyists in particular eschew boilerplate code, but I think it can obfuscate the intention of a class as well as giving you behavior you may not want/need (== / hash / default constructor). Struct is great for things explicitly needing that behavior - value objects.
Anyway, that’s my personal soap box on the matter. There’s no performance justification for my opinion there, so I can’t as-objectively say my way is “right”. This article points out a really clear, objective reason to at least avoid Struct.new(…).new(…), which I think everyone can get behind.
i overuse OpenStruct to avoid writing anything :) makes the code really pleasant to iterate, and i usually refactor to use an explicit struct once the design has crystallised.
I might go a step further and suggest people overuse
Struct
to avoid writingattr_accessor
. It’s understandable since Rubyists in particular eschew boilerplate code, but I think it can obfuscate the intention of a class as well as giving you behavior you may not want/need (==
/hash
/ default constructor).Struct
is great for things explicitly needing that behavior - value objects.Anyway, that’s my personal soap box on the matter. There’s no performance justification for my opinion there, so I can’t as-objectively say my way is “right”. This article points out a really clear, objective reason to at least avoid
Struct.new(…).new(…)
, which I think everyone can get behind.i overuse
OpenStruct
to avoid writing anything :) makes the code really pleasant to iterate, and i usually refactor to use an explicit struct once the design has crystallised.