4 ms·
The only thing I really miss in the default Ruby hash is property access for keys like JavaScript has. I know, I know, OStruct does this but it's not the thing
by blarnopre 3y ago
The only thing I really miss in the default Ruby hash is property access for keys like JavaScript has.
I know, I know, OStruct does this but it's not the thing that gets built from hash literals so it's less convenient.
It's a minor quibble
- lloeki 3y agoOpenStructs 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