From: Jean-Hugues ROBERT Date: 2002-06-18T00:51:06+09:00 Subject: yield and recursion, how to ? Hello, I am trying to implement an iterator for a recursive structure. After too much effort I got: class Test attr_accessor :name attr_accessor :children def initialize( name ) @children = [] @name = name end def each( block = nil, &syntax_block ) block ||= syntax_block block.call( self, @children.length()) @children.each do |x| x.each block end end end o = Test.new( "Foo") o.children << Test.new( "Bar") o.each do |x,y| p x.name, y end This works OK, but.... this does work as I expected: def test() o.each do |x,y| p x.name, y break if name == "Foo" # Should break, does not return if name == "Foo" # Should return, does not end end test() Apparently "break" does not break ("return" does not return) unless some "yield" is involved. However I could not figure out how to rewrite Test##each() using yield. What am I missing here ? Thanks, Jean-Hugues