11 ms·
OpenStructs are ungodly slow anyway. If you want such accessors you probably want a plain Struct anyway and not mix hashness and method accessors. You can even
by lloeki 3y ago
OpenStructs are ungodly slow anyway. If you want such accessors you probably want a plain Struct anyway and not mix hashness and method accessors. You can even define additional methods on structs:
Foo = Struct.new(:a, :b, keyword_init: true) do
def frobz
...
end
end
Structs are quicker to instantiate than classes but slower to call methods on, so if you instantiate a lot but seldom call they're great. Otherwise a class is better.
- ljm 3y agoThe new Data class in Ruby 3.2 also does the job (although it's intended for immutable data) https://docs.ruby-lang.org/en/3.2/Data.html https://docs.ruby-lang.org/en/3.2/Data.html