From: Benjamin Kudria Date: 2010-07-08T13:18:44+09:00 Subject: Re: Extending OpenStruct to be lazy and recursive Michael Jackson wrote: > I've done something similar that's pretty lightweight. My version > actually extends Hash, so it should be pretty fast and you get all the > niceties of Hash for free. Michael, thanks for the pointer, but I don't really see how it helps. Your code doesn't support recursive conversion, or lazy property retrieval. I'm content with using OpenStruct for all the functionality your code provides. Thomas Sawyer wrote: > Not 100%, but maybe have a look at Hashery's OpenCascade for some > ideas (http://rubyworks.github.com/hashery). I think you can do the > lazy lookup without using a Promise. Indeed, my initial version didn't use mental's library, but I had to reach way deeper into OpenStruct to make the appropriate changes. OpenCascade is awesome! But,I left out a small detail of my implementation that is actually rather important - my to_self method actually tries to find a class corresponding to the name of the key (the .classify.constantize call) and use that if available. So, {:authors => [{:name => 'Poe'}, {:name => 'Lovecraft'}]} would actually end up converting to two Author objects, if the class were available, passing the hash to the initialize method. This functionality is important to me, it allows me to hang business logic on sub-documents in my overall document. OpenCascade, it seems, converts only to OpenCascade. (I may take a detour and subclass OpenCascade instead, modify it to support my auto-classifying,, and compare the two approaches.) Fortunately, mental clued me into the problem with my code [1] shortly after I posted this. For future reference, I was creating the promise (with its associated block) and closing over the same k and v variables, instead of creating a new scope every time. I separated the call out to a method, and everything worked. Benjamin Kudria ben@kudria.net 1: http://twitter.com/mentalguy/status/17993170022 and http://twitter.com/mentalguy/status/17993202421 -- Posted via http://www.ruby-forum.com/.