1

They both seem to accomplish the same thing. What use cases would require one over the other?

Currently, I'm working on some presenter classes. My first thought was to use OpenStruct, but I can't identify a meaningful difference between using a Hash and an OpenStruct.

Trip
  • 26,756
  • 46
  • 158
  • 277

2 Answers2

4
  1. Object syntax v. hash syntax.

If a method requires an object with certain accessors, then a Hash would not work.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
7stud
  • 46,922
  • 14
  • 101
  • 127
1

OpenStructs are sloooooooooow and memory intensive , and don't scale well for large data sets. Creating 1 million OpenStructs is ~100x slower than creating 1 million Hashes.

This has been discussed in detail here:

When should I use Struct vs. OpenStruct?

Community
  • 1
  • 1
Tilo
  • 33,354
  • 5
  • 79
  • 106
  • Hmm, so you recommend sticking to Hashes then? Why do people use Structs/OpenStructs then? – Trip Apr 19 '13 at 00:21
  • 1
    Yes, creating 1 million Openstructs is about 100x slower than creating 1 million Hashes. Please check out the detailed comparison in that question I mentioned. – Tilo Apr 19 '13 at 02:59
  • Wow thanks so much Tilo. I guess OpenStructs is just a strange niche case that is rarely used for very rare circumstances. Thanks! – Trip Apr 19 '13 at 12:37