Sha256: 9e71103399f44bdc71847b17ff91442fa65407dbd561f8443998886ab75dd2b3

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

# Copyright: Copyright (c) 2004  Nicolas Pouillard. All rights reserved.
# Author: Nicolas Pouillard  <ertai@lrde.epita.fr>.
# 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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
ttk-0.1.576 ruby_ex/top_down.rb
ttk-0.1.580 ruby_ex/top_down.rb
ttk-0.1.579 ruby_ex/top_down.rb
vcs-0.2.148 ruby_ex/top_down.rb