
CommunityNews
Ruby Structs
Ruby’s
Struct
is one of several powerful core classes which is often overlooked and under utilized compared to the more popularHash
class. This is a shame and I’m often surprised when working with others who don’t know about structs or, worse, abuse them entirely. I’d like to set the record straight by sharing the joy of structs with you and how you can leverage their power to improve your Ruby code further.
Read in full here:
This thread was posted by one of our members via one of our news source trackers.
Most Liked

AstonJ
From SO:
OpenStruct
objects are useful when you need something to fit a certain method call interface (i.e. send in a duck-typed object responding to#name
and#value
), or when you want to encapsulate the implementation details, but also want to avoid over-engineering the solution. They also make an awesome stub object, and I often use them in place of framework stubs when I don’t need the overhead of a stub/mock.
You’ll just have to get over your dislike of Ruby and write a few apps in it ODL - just so you can see why so many people love it

Maartz
Same. I’ve never used Ruby Structs… I learn about them like a week ago in this post from Alchemist up above.
Also never encountered it in any codebase so far…

Maartz
True!
Finally, we have
OpenStruct
which is part of Ruby Core as well. At this point, you might be thinking: “Hey, looks like anOpenStruct
is better in terms of meldingStruct
andClass
syntax and functionality.” Well, you’d be very wrong in terms of performance but, as mentioned withClass
usage, I promise to expand upon this more later.
Warming up --------------------------------------
Array 2.000 i/100ms
Hash 1.000 i/100ms
Struct 1.000 i/100ms
OpenStruct 1.000 i/100ms
Class 1.000 I/100ms
Calculating -------------------------------------
Array 23.854 (± 0.0%) i/s - 120.000 in 5.030763s
Hash 8.908 (±11.2%) i/s - 45.000 in 5.072652s
Struct 4.798 (± 0.0%) i/s - 24.000 in 5.005263s
OpenStruct 0.178 (± 0.0%) i/s - 1.000 in 5.618325s
Class 4.230 (± 0.0%) i/s - 22.000 in 5.203019s
Comparison:
Array: 23.9 i/s
Hash: 8.9 i/s - 2.68x (± 0.00) slower
Struct: 4.8 i/s - 4.97x (± 0.00) slower
Class: 4.2 i/s - 5.64x (± 0.00) slower
OpenStruct: 0.2 i/s - 134.02x (± 0.00) slower
Still it seems that Structs are faster, but if it was, why is not idiomatic to use them ?
And use a class for “singleton instance” and struct for the rest like in Swift.
Popular Backend topics










Other popular topics










Latest in Backend
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /zig
- /scala
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /julia
- /c
- /slackware
- /neovim