Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Object
- def walk(&block)
- walk_object(Set.new, &block)
- end
- def walk_object(set, &block)
- return false if set.include?(self.object_id)
- set << self.object_id
- yield self
- walk_subobjects(set, &block)
- end
- def walk_subobjects(set, &block)
- instance_variables.each { |name|
- o = obj.instance_variable_get(name)
- o.walk_object(set, &block)
- }
- end
- end
- class Array
- def walk_subobjects(set, &block)
- super
- self.each { |o| o.walk_object(set, &block) }
- end
- end
- class Hash
- def walk_subobjects(set, &block)
- super
- keys.walk_subobjects(set, &block)
- values.walk_subobjects(set, &block)
- end
- end
Add Comment
Please, Sign In to add comment