From: Christoph Rippel Date: 2001-03-07T03:10:02+09:00 Subject: [ruby-talk:12168] Re: disassembling and reassembling a hash "Raja S." wrote in message news:982srj$1p1$1@jetsam.uits.indiana.edu... > 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. Actually, I am wondering about your interpretation - you are actually going down-wards I believe - If Hash would not be an object of type Class you would be anonymously sub-typing (creating a singleton class) but since Hash is already a Class object non (anonymous) sub-class is created - but I am not really sure about this ... [...] > 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). I agree - you should post this as a RCR IMO (I would prefer a new class function in Hash like Hash.make(keys,values)) Christoph [...]