From: Sean O'Dell Date: 2004-06-06T14:47:31+09:00 Subject: Re: How to ducktype a Hash? On Saturday 05 June 2004 22:18, Dave Thomas wrote: > On Jun 6, 2004, at 0:06, Sean O'Dell wrote: > > The objects are not always a Hash per se, just objects with hash-like > > functionality. But, as I said, other objects can slip in too, like > > strings > > and arrays, which have [] and []= methods, but which don't act like > > hashes at > > all. > > What gives them this hash-like functionality? Can you test for it? And > why would invalid objects be passed in? It's tough to explain, the code affecting this is spread out in different places. The code comes in as a tree, sort of like YAML. Actually, sometimes it IS YAML, but sometimes it's just a tree of data (hashes, arrays, string, other objects) that comes piped in from other scripts (my code is the server, the other scripts are clients). The server needs certain points in the code to be hashes, that is, to contain other objects and index them using other objects, string, etc. Usually they're hashes, but sometimes they're these other objects which I wrote that have hash-like functionality. The only thing I know to test for using just built-in Ruby is [] and []=. Unfortunately, if data comes in sport goofy, I get strings, arrays and others things in the wrong places, and they sometimes also have [] and []=, so I can't id them that way. One test I can to really solve this problem is to create artificial tags for Hashes, my objects and any other objects that get created either by me or someone else as Hashes. That solution will work, but my complaint is that it feels like such a kludge. My problem is solved though -- I have several choices now. I just wish Ruby did this already. I am getting bit by little stupid things that are hard to catch, and my growing project is now starting to feel REALLY unstable. I never get that feeling with C++, so I guess I should stop trying to use Ruby for these larger projects. This one though, it started so nicely and fast...but now, it feels sort of kludgy and I don't have a feeling of confidence about its ability to either perform properly, or at least report errors that I consider errors. Since there is no typing mechanism at all, I can't tell Ruby "hey, anything but a hash-like object here is an error." It's just frustrating now, that's all. Sean O'Dell