From: Iain Barnett Date: 2012-06-21T23:26:46+09:00 Subject: Re: Looking for a better way to add a method to a core class than monkey patching On 21 Jun 2012, at 14:30, Robert Klemme wrote: > On Thu, Jun 21, 2012 at 2:33 PM, Iain Barnett wrote: > >> If you're still interested, the code in question is at https://github.com/yb66/sequel/blob/master/lib/sequel/extensions/pg_hstore.rb. The method(s) would be a Hash#to_hstore and a String#from_hstore. When I get a chance I'm going to make it into its own gem (on the advice of the maintainer) and I'll consider whether it's a good idea to continue with the monkey patches. > > I my world String#from_hstore would be a class method at least. I > would also not cache @result in the String because that will prompt > all sorts of inconsistency issues when the String is changed. > Actually I'd rather have method HStore.from_string(str) and > HStore#to_string or maybe even HStore.from_db_string(str) and > HStore#to_db_string. > > I am not sure I understand yet why you have three types involved: > String, Hash and HStore. You certainly need String and HStore. But I > would consider conversion to or from Hash just as a convenience > feature of HStore i.e. to easily create one. That would make changing > Hash superfluous. And all conversion methods could go into HStore as > explained above. > > Btw, you can turn > > token_pairs = token_pairs.map { |k,v| > [k,v].map { |t| > case t > > into > > token_pairs = token_pairs.map { |a| > a.map { |t| > case t > > and save an Array creation. > > Cheers > > robert > Thanks Robert, I'm grateful for the advice. Regards, Iain