From: Trans Date: 2007-01-18T23:05:57+09:00 Subject: Re: Hash#open! / Hash#close! Robert Dober wrote: > Hmm is it really an oddity (I have the same behavior on 1.8.5/debian)? > An empty hash sort just cannot apply the block to any elements, but I guess > you know that ;). > Would you prefer that sort_by just checks for the presence of a block > anyway? No, I think I'd rather it default to a nominal { |x| x } block, or something. but yea, it's not a big deal, just sort of suprised me. > I gotto think a lot about the rest you have written most interesting stuff, > just some very quick thoughts > * There are structs you know ;) > * #open! could return self so that one can write h={:value=>42}.open! good idea. > #close! should than too for symmetry. > * Would it not be better not to hide a method in case the hash has no key? > That would be a mess right? One would not know if h.size is 1 because of > h[:size]=1 or h={:a=>1}.open! > > * I'd like to use #open! behind the scenes with a block form like > class Hash > def use_opened &blk > open! > blk.call(self) if blk > close! > end > end > or even > class Hash > def in_context &blk > open! > instance_eval &blk > ..... > > Conclusion: > Basically it is a nice idea but maybe you really want a struct. > I would not think it should be in the core but in an extension like facet > of course it might > be spot on ;) Good points. probably right about the struct. maybe i should eleborate on why i thought of this at all... i like to be as flexiable as possible, in my current project i have many methods that take a single hash (or "ducked-hash") argument. i know what to expect in the argument and i want to access the data with a method notation rather than hash['key'] notation. def amethod( hashything={} ) hashything.foo end it's not just a matter of presonal preference either. even if i left it as a hash i would need to massage the data to make sure all the keys are strings or symbols: def amethod( hashything={} ) hashything = hashything.rekey(:to_s) hashything['foo'] end so either way i have to effect the argument in some manner. hence i considered the idea of open!/close! def amethod( hashything={} ) hashything.open! hashything.foo hashything.close! end as you suggested, a block form might be better. of course i could just go back to conveting the hashything to an OpenStruct (actually I use Facets' OpenObject) but i was just wondering if maybe there's a nicer/easier/conciser way. ideally, i wish i didn't have to even worry about this. > In any case that is the kind of post which make this ML so much fun and > education. glad to hear :-) t.