Robert Klemme
4/29/2005 3:01:00 PM
"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1114716185.239134.214060@o13g2000cwo.googlegroups.com...
> ruby-talk:64146 is from over 2 years ago. I wonder if any progress on
> this idea has been made. Ruby 2.0?
Hm, why wait? You can cook one on your own:
Regards
robert
require 'set'
class Traversal
include Enumerable
def self.traverse(obj,&b)
v = self.new obj
v.each &b
v
end
def self.next_objects(obj)
case obj
when Hash
obj.keys + obj.values
when String
[]
when Enumerable
obj
else
obj.instance_variables.map{|v| obj.instance_variable_get v}
end
end
def initialize(obj)
@start = obj
end
def each
visited = Set.new
queue = [@start]
until queue.empty?
n = queue.shift
visited << n.object_id
tmp = yield n
# pp tmp
tmp.each {|ne| queue << ne unless visited.include? ne.object_id}
end
self
end
end
require 'pp'
h = {1=>"one", 2=>"two", 3=>"three"}
h[4] = h[2]
h[5] = h
pp h
puts "---------------"
Traversal.traverse h do |obj|
# pp obj.object_id, obj
pp obj
Traversal.next_objects obj
end