From: "trans. (T. Onoma)" Date: 2004-11-15T10:22:50+09:00 Subject: Re: OpenStruct#update ? On Sunday 14 November 2004 06:16 pm, Yukihiro Matsumoto wrote: | Hi, | | It's amazing that OpenStruct which is a proof of concept tiny toy | attracts so many users. It is a very convienet way to access data that is object polymorphic. Problem, of course, is namespace clash with core methods. What might help: 1) a "kernelless" object class which reduces the core methods to bare minimum. 2) Have specialized core aliases like __id__ and __send__ (e.g. __class__) for all methods considered core. These *cannot* be overridden ever. And 3) have private and public methods have separate namespaces --could that help? Might that even boost method lookup speeds? Just some thoughts on the matter. On the whole I'd rather be able to override methods. | In message "Re: OpenStruct#update ?" | | on Mon, 15 Nov 2004 06:07:11 +0900, "trans. (T. Onoma)" writes: | |Hmm.. I also just had another related thought. I might also be useful if a | |normal object could be "OpenStruct'd". I have a need for it at the moment. | |Currently I take an object in as input and then dynamically define | | instance vars with accessors on it as required, but often no initial | | object is given in which case an OpenStruct is much easier to use. I'd | | rather just have the OpenStruct capability from the get go. | | Can you be more specific, preferably with code example? Sure. Okay first, basically what I was doing, given object o and data in hash h: h.each do |k,v| o.instance_variable_set("@#{k}", v) o.instance_eval <<-EOS def #{k}; @#{k}; end def #{k}=(x); @#{k}=x; end EOS end Now if o is an OpenStruct, the above is much easier, well, easier after I've added the before mentioned #update method: o.update( h ) But o might not be an OpenStruct --the library user may have a pre-made object that they want to use. What might be of use then, rather then a stand alone OpenStruct object is a way to "open" a regular object: o.extend OpenStruct Actually, if one could extend using a class, instead of just a module, I think it might almost work just like that. Thanks, T.