From: Jim Weirich Date: 2004-12-05T09:03:39+09:00 Subject: Re: Using yield On Saturday 04 December 2004 05:44 pm, Bill Kelly wrote: > My most recent use of yield is in a tree-structured database, > where I wanted to be able to perform {some operation} on a > node and all its children. �So, > > � def apply_all(&block) > � � yield self > � � @children.each_value {|n| n.apply_all(&block) } if @children > � end This is a very common use of blocks. You might consider the following modification, it may (or may not) be useful in your application. Change the name of "apply_all" to "each", and then include the module Enumerable into your class. Then you can do things like (assuming "name" might be a method on your node) .... some_node.collect { |n| n.name } # Get a list of all the names some_node.find { |n| n.name =~ /^Bill/ } # Find a matching node -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)