RubyFlow The Ruby and Rails community linklog

OpenStruct 2

I just launched ostruct2 gem intended as drop in replacement for Ruby’s standard ostruct.rb library. The original OpenStruct class has some short-comings, such as name conflicts with built-in methods. OStruct2 improves upon the original by subclassing BasicObject to ensure a clean slate and adds some additional niceties such as support for Enumerable. This is v0.1.0. I’m sure it needs a little bit more work to be complete (e.g. #hash and #dup methods are probably unavoidable), but all the primary functionality is green.

Comments

Interesting implementation. Do you think it is a good idea to expose internal state(@table) when using bang! methods? If the implementation of @table changes in the future code that relies upon it may be broken. That’s why it might be better to only provide a public interface that manipulates internal data structures.

Anytime I see code trying to clean up standard lib, I say bravo!

@robgleeson @table is not exposed, I don’t see where it is.

@robogleeson You are right. I mention this issue in the current docs. Ultimately there needs to be some interface between the method_missing-handled bang methods and the underlying table, however I haven’t yet figured out the best approach. It would be nice if there were a way to do it dynamically rather than having to write a delegating method for every single possibility. I will get to this as soon as I can. If you have any suggestions for handling please let me know. In the mean time, I do not think it will matter for most use cases, which is why I decided it was okay for initial release.

@jim It’s exposed in #method_missing if you use a bang method that is not defined it will delegate it to the underlying table.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in