From: Ceri Storey Date: 2004-04-04T18:11:35+09:00 Subject: Re: recursion w/ blocks; *this On Sun, Apr 04, 2004 at 05:58:04PM +0900, Nicholas Paul Johnson wrote: > Hello again, > > First is, does Ruby have anything equivalent to C++'s `this' pointer? > I've tried the docs, but don't know what to look for other than 'this'... Aye. You're looking for the 'self'. eg: irb(main):001:0> self => main > Second, if I were to write a method that accepts a block, and would like > to be able to recurse in that method while allowing each of the recursions > to use the same block, how would I do that? def a_method(arg, &block) if arg > 10 a_method(arg.succ, &block) end end More details here: http://rubycentral.com/book/tut_methods.html > As an example, suppose I have a tree data structure (below), such that > each node has two children: a and b. I want to create an ``each'' method. > Ideally, I would yield with ``this,'' and then would recurse to ``each'' > on every child of that node. class Node def initialize(left, right) @left = left @right = @right end def each(&block) block.call(self) @left.each(&block) if @left @right.each(&block) if @left end end -- Ceri Storey