Closed banister closed 7 years ago
I originally wrote this gem 8 years ago, and it was a relatively simple subclass of OpenStruct back then (although had more lines of code than your example above, since I added the ability to optionally recurse over arrays). Since then, other developers started using it to mutate nested hash values and ran into various bugs with doing that (one of which is still open). Addressing those bugs has added significantly to the complexity of ROS.
I agree that it seems more complicated than it should be, and an approach like your function would work for what I think is probably the majority of use-cases and then. I don't really have the time or inclination to figure out how to best rewrite ROS (if it can be simplified and still mostly conform to OpenStruct's API), and I am mainly just making sure it works with new versions of Ruby and accepting bug fixes.
Ah that makes sense, thanks :) However (unless i'm misunderstanding again) the above code also recurses over arrays:
obj = convert_to_object(a: 1, b: 2, c: [{j: 1}, {k: 2}])
obj.c.first.j #=> 1
obj.c.last.k #=> 2
Yeah, your code recurses over arrays and works nicely for a read-only use case, but the feature is optional for ROS (although there is probably a strong case to be made to just make it the default).
I'm sure i'm missing something, but why can't you just use a simple recursive function to convert a nested hash to nested open structs? e.g
Use it like so: