Show me the ruby way

metaData = Hash.new {|h, k| h[k] = Hash.new(0) }

Perfect.

B.Candler> Hash.new { |h1,k1| h1[k1] = Hash.new {

h2,k2| h2[k2]=0 } }

Even better!

Is there an obvious way to implement this
initializtion within the SpecialCaseHash class:

def new
super.new {|h, k| h[k] = Hash.new(0) }
end

doesn’t seem to to it.

Thank you.

···

nord

#!/bin/ruby
class SpecialCaseHash < Hash
def printSortedBy(sortSpecifier=‘keys’)
puts “---- sorted by #{sortSpecifier}”
self.keys.sort.each do | m |
puts m
if sortSpecifier == ‘keys’
self[m].keys.sort_by {|a| a }.each do | k |
puts " #{k.ljust(12)} #{self[m][k]}"
end
elsif sortSpecifier == ‘vals’
self[m].keys.sort_by {|a| -self[m][a] }.each
do | k |
puts " #{k.ljust(12)} #{self[m][k]}"
end
end
end
end

def printByKeys
printSortedBy(‘keys’)
end

def printByVals
printSortedBy(‘vals’)
end
end

metaData = SpecialCaseHash.new {|h, k| h[k] =
Hash.new(0) }
metaData[‘fruit’][‘apple’] = 13
metaData[‘fruit’][‘mango’] = 7
metaData[‘fruit’][‘banana’] = 11
metaData[‘fruit’][‘cherry’] = 17
metaData[‘veg’][‘eggplant’] = 7
metaData[‘veg’][‘artichoke’] += 19
metaData[‘veg’][‘green bean’] -= 5
metaData[‘veg’][‘squash’] += 2
metaData.printByKeys
metaData.printByVals


Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

I think that should be just super, not super.new:

def new
super { |h, k| h[k] = Hash.new { |h, k| h[k] = 0 } }
end

-Mark

···

On Wed, Sep 03, 2003 at 05:17:41AM +0900, nord ehacedod wrote:

Is there an obvious way to implement this
initializtion within the SpecialCaseHash class:

def new
super.new {|h, k| h[k] = Hash.new(0) }
end