CommunityNews

CommunityNews

Ruby Structs

Ruby’s Struct is one of several powerful core classes which is often overlooked and under utilized compared to the more popular Hash 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. :rocket:

Read in full here:

This thread was posted by one of our members via one of our news source trackers.

Most Liked

AstonJ

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 :003: :lol:

Maartz

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

Maartz

True!

Finally, we have OpenStruct which is part of Ruby Core as well. At this point, you might be thinking: “Hey, looks like an OpenStruct is better in terms of melding Struct and Classsyntax and functionality.” Well, you’d be very wrong in terms of performance but, as mentioned with Class 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 Top

First poster: bot
Such inflammatory, much wow. Unfortunately, Haskell itself agrees. Some languages naturally lend themselves towards adoption. Some don’t...
New
First poster: AstonJ
Ten years without Elixir. I never got into Elixir, largely because it looked like Ruby. I was a Rubyist for a good while, spent time and...
New
First poster: bot
Django 3.2 is just around the corner and it’s packed with new features. Django versions are usually not that exciting (it’s a good thing!...
New
First poster: Exadra37
Summary: I describe a simple interview problem (counting frequencies of unique words), solve it in various languages, and compare perform...
New
First poster: AstonJ
They expect you to make a onepage application (SPA) The polaris design system officially only supports react Integration with the s...
New
First poster: bot
Like, on a scale from c to rust? issue c zig (release-safe) rust (release) out-of-bounds heap read/write none runtime runtime ...
New
First poster: brennan
The perspective of an ignorant computer science undergrad It’s likely that you read the title of this post and thought “what is this guy ...
New
First poster: bot
Just a small test with lists in cython. Considering echosystem, multithreading and ease of use, Julia is a clear winner here.
New
CommunityNews
This thread was posted by one of our members via one of our news source trackers.
New
brainlid
In episode 78 of Thinking Elixir, we talk with Chase Granberry about Logflare. We learn why Chase started the company, what Logflare does...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1020 17002 374
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
New