lib/core/facets/hash/autonew.rb in facets-2.8.4 vs lib/core/facets/hash/autonew.rb in facets-2.9.0.pre.1

- old
+ new

@@ -1,25 +1,23 @@ class Hash - # Hash which auto initializes it's children. + # Hash which auto initializes it's children. # - # ah = Hash.autonew - # ah['section one']['param one'] = 4 - # ah['section one']['param two'] = 5 - # ah['section one']['param three'] = 2 - # ah['section one']['param four'] = 3 + # h = Hash.autonew + # h['s1']['p1'] = 4 + # h['s1']['p2'] = 5 + # h['s1']['p3'] = 2 + # h['s1']['p4'] = 3 # - # p ah - # # {"section one"=>{"param one"=>4, "param four"=>3, "param three"=>2, "param two"=>5}} + # h #=> {"s1"=>{"p1"=>4, "p4"=>3, "p3"=>2, "p2"=>5}} # - # p ah['section one'].keys - # # ["param one", "param four", "param three", "param two"] + # h['s1'].keys.sort + # #=> ["p1", "p2", "p3", "p4"] # # CREDIT: Trans, Jan Molic def self.autonew(*args) - #new(*args){|a,k| a[k] = self.class::new(*args)} - leet = lambda { |hsh, key| hsh[key] = new( &leet ) } + leet = lambda{ |hsh, key| hsh[key] = new( &leet ) } new(*args,&leet) end end