# Copyright: Copyright (c) 2004 Nicolas Pouillard. All rights reserved. # Author: Nicolas Pouillard . # License: Gnu General Public License. # $LastChangedBy: ertai $ # $Id: top_down.rb 53 2004-12-02 22:24:03Z ertai $ class Object def top_down(&block) yield self end def top_down_map(&block) yield self end end class Hash def top_down(&block) yield self each { |k,v| v.top_down(&block) } end def top_down_map(&block) yield self res = {} each { |k,v| res[k] = v.top_down_map(&block) } res end end class Array def top_down(&block) yield self each { |x| x.top_down(&block) } end def top_down_map(&block) yield self res = [] each { |x| res << x.top_down_map(&block) } res end end # # Driver # #FIXME: make me unit test instead of mini-driver if __FILE__ == $0 a = { 'toto' => [ 1, 2, 3 ], 'tata' => { 'foo' => 42, 'bar' => 43 } } a.top_down { |x| p x } b = a.top_down_map { |x| x.is_a?(Numeric) ? x + 1 : x } p b end