From: raja@... (Raja S.) Date: 2001-03-07T00:40:03+09:00 Subject: [ruby-talk:12143] Re: disassembling and reassembling a hash ts writes: >R> keys,values = h.keys, h.values # disassemble >R> new_vals = norm_v (values) # normalize >R> Hash.make (keys, new_vals) # reassemble > h1 = {} > h.each { |k, v| h1[k] = norm_v(v) } > no ? Not quite. v[i][j] is normalized based on -all- the values in v not just those in v[i]. Hence in the above norm_v (values) takes a vector of vectors formed from the values of the hash. >>> p.s. is there a way to override a class method like Hash.new -without- >>> sub-classing? It appears that class methods can't be aliased hence >>> redefinition of something like Hash.new doesn't seem possible? > class Hash > class << self ... Aha! 'self' now refers to the class 'Hash' and by creating an anonymous super-class for it, class methods of 'Hash' become its instance methods and hence can be aliased. I didn't want to go "downwards" in terms of creating a sub-class of Hash but didn't occur to me that one could go "upwards" and create an anonymous super-class! Very nice and very clever! Thank you. The brilliance of matz comes thru again in his design choices and how harmoniously the concepts of Ruby hang together! Now I've got: class Hash class << self alias :old_new :new def new (*args) if ((args.length==2) && (args[0].type==Array) && (args[1].type==Array) # && (args[0].length == args[1].length) # if more keys pad with nil; if more values, ignore ) h={} keys, vals = *args keys.each_with_index { |k, i| h[k] = vals[i] } return h else old_new (*args) end end end end Question: since we have #to_a, #keys, #values --- all used to disassemble a hash would it be useful to enhance Hash.new(key,values) in the core to assemble a hash from an array of keys and values? Shouldn't break anything existing (I think). Question: my original question on the order of the entries returned by #keys and #values remains. Raja