From: Todd Benson Date: 2008-05-15T01:39:07+09:00 Subject: Re: changing hash key On Wed, May 14, 2008 at 10:24 AM, Tim Wolak wrote: > I posted that code after I fixed an error. It is getting initialized, > after I iterate over the hash its still printing out the numbers that > are account numbers for the keys. I need to get any numbers that are > less than 39 and put that in a new hash with the key as SKTY and all > values added together into a value for that key. > > Hope that make more sense of that I'm trying to do. > > Tim Let me see. You want to have a new hash called skty with keys skty and sktny? That's confusing, mostly I think because of your choice of variable names. If you can be sure that all keys are comparable with each other (in this case integers), here's very quick and dirty... class Hash def sum_values values.inject {|s, i| s + i} end def partition_by part h1 = self.reject {|k,v| k > part} h2 = self.reject {|k,v| k <= part} return h1, h2 end end h = Hash[1,5,2,6,3,7,4,8,5,9] a, b = h.partition_by 3.5 h_new = {} h_new['skty'] = a.sum_values h_new['sktny'] = b.sum_values p h_new You could most of this with one line if you were so crazily inclined :-) Todd