From: Joel VanderWerf Date: 2001-09-01T10:30:10+09:00 Subject: [ruby-talk:20652] Re: Iterating by links David Alan Black wrote: > I'm still playing around with this :-) but one possible source of > problems is that you use a boolean test to end the series... which > means, for example, that if you change this: > >> tree = [[0, [1, 2]], 3, [4, 5, [6]]] > > to this: > > tree = [[0, [1, 2]], 3, [4, 5, [6,7,nil]]] > > it will not process tree[2][2][2]. Hi, David, Good point. Maybe it should automatically check for respond_to? before sending the message. You can do that manually: tree = [[0, [1, 2]], 3, [4, 5, [6, 7, nil]]] for node in tree.by { |x| x.respond_to?(:at) && x.at(2) } p node end To handle this automatically, maybe catching an exception is faster than checking for respond_to? How about this change: def each cur = @first if @next_name begin loop do break unless cur yield cur cur = cur.send @next_name, *@args end rescue NameError end elsif @next_proc .... Although I wouldn't want to do this without checking somehow that the exception was generated by the send in this method, rather than something called as a result of the send operation. Failing that, we could put respond_to? inside the loop. I'll do some benchmarks to see if that's too bad. Joel