Last Updated: February 25, 2016
·
1.012K
· krainboltgreene

OpenStruct Pull Request [WIP]

Hello Ruby Core,

In this Pull Request I have made a series of changes that I would like to be added to the Ruby stdlib library ostruct.
Before getting into anything I want to stress one thing: All changes are mostly backwards compatible with the previous ostruct library. Any non-compatible changes are what I consider semi-bug fixes.

First a list of problems with the current OpenStruct class:

  • OpenStruct is a class, but often is only used to give the flexible behavior to children classes. The OpenStruct behavior should be mixed into other classes as a Module. However there are still use cases of just wanting a plain OpenStruct object.
  • OpenStruct behaves similarly to a Hash. OpenStruct can be built from, updated with, and dump a Hash. It even has a similar method to Hash#delete(), the OpenStruct#delete_field() method. Yet the OpenStruct is missing a few key methods like merge or merge!. These methods would work exactly like the (already defined) OpenStruct#marshal_load(). I believe that OpenStruct would benefit from aliases that make it behave like Hash and abide by the Principle Of Least Surprise.
  • All of the methods on the OpenStruct are open to easy overwriting. In addition some of the methods use other methods that are likely to be overwritten like the call to OpenStruct#object_id in the OpenStruct#inspect method. It also causes problems like this: https://gist.github.com/3460906

I started working on an alternative to OpenStruct after having some problems with it.
Instead of modifying OpenStruct it was simply easier to build my own version.
After a while I abandoned it until recently when I needed OpenStruct again.
I decided to finish my work, upgraded with what I've learned since I started.
I've done a few things in this pull request:

  • I've fixed at least 3 bugs:
    • Using the OpenStruct#delete_field method wouldn't actually remove the method given, only the table key/value. This creates a confusing mismatch in the object and the table.
    • When using OpenStruct#marshal_load it would correctly create getter and setter methods for each key/value. However it would also delete the entire table.
    • One of the tests freezes an OpenStruct object and then redefines OpenStruct#frozen? and it works. Freezing should stop changing the object, in any way.
  • I've made a series of important improvements as well:
    • To fulfill the requirements of the first problem I have with OpenStruct I have created a module nested in the OpenStruct class called M. That module contains all of the behavior and can be included when needed.
    • I've designed that all the public methods are wrapped in double underscores in the same way that Object#__send__ and many other important methods are named. In addition I have aliased many methods to their "underscore" forms (ex OpenStruct#inspect aliased to OpenStruct#__inspect__) to avoid confusion. Finally I've aliased "underscore" methods to the important methods like OpenStruct#__freeze__ to OpenStruct#freeze. Because of the nature of aliasmethod you can now define a key/value "objectid" and still have access to the original OpenStruct#object_id with OpenStruct#__object_id__. This avoids the bug listed in the gist: https://gist.github.com/3460906
    • I have added the ability to only dump a specific set of keys with the OpenStruct#__dump__ method.
  • Finally I have improved the tests for this library by either breaking them up into manageable pieces or refined them to read better. There are now test for other features of OpenStruct as well as tests for the new features added in this pull request.

Finally with those things listed I only have the benchmarks to show.
I used two types of benchmarking, both Benchmark#bmbm and the benchmark-ips gem's Benchmark#ips.
Both show favorable results for my version of ostruct.
You will notice that my version is labeled AltStruct, because it is also a gem
Here are links to the results: https://gist.github.com/3462357