Recursive methods allowed in Ruby?

Does Ruby allow recursive methods? For example, in Java you can have a
method call itself, and an example such as the one below will work. When I
try to run the following Ruby example, I get a “stack too deep” error. (Or
did I just make a stupid mistake?)

Thanks,
Krishna

···

def list_dirs( inDir )
Dir.foreach( inDir ) {|x|
print( x )
if File.ftype( x ) == "directory"
list_dirs( x )
end
}
end

ARGV.each{ |dirName|
list_dirs(dirName)
}

Dir.foreach lists “.” (current directory" and “…” (parent directory) which
is why you are getting infinite recursion. Try adding:
next if x == “.” or x == “…”

To make your program work you will also need to chdir() into each directory,
or else build up a pathname, so that directories within directories are
found. Something like this:

path = inDir + “/” + x
if File.ftype( path ) == “directory”
list_dirs( path )
end

Regards,

Brian.

···

On Wed, Mar 26, 2003 at 05:50:47AM +0900, Krishna Dole wrote:

def list_dirs( inDir )
Dir.foreach( inDir ) {|x|
print( x )
if File.ftype( x ) == “directory”
list_dirs( x )
end
}
end

ARGV.each{ |dirName|
list_dirs(dirName)
}

I assume you run into an endless loop on ‘.’, the current directory.

list_dirs ‘somedir’
=> x = ‘.’
‘.’ is directory => list_dirs ‘.’
=> x = ‘.’
‘.’ is directory => list_dirs ‘.’

 ... etc ...

-Martin

···

On Wed, Mar 26, 2003 at 05:50:47AM +0900, Krishna Dole wrote:

When I
try to run the following Ruby example, I get a “stack too deep” error. (Or
did I just make a stupid mistake?)
(… snipsnap …)

For this particular case, it is much easier to use class Find (require
“find”, p. 450 of Pickaxe) to do directory traversing for you. It works
similar to Unix ‘find’ command.

Gennady.

···

----- Original Message -----
From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, March 25, 2003 1:01 PM
Subject: Re: Recursive methods allowed in Ruby?

On Wed, Mar 26, 2003 at 05:50:47AM +0900, Krishna Dole wrote:

def list_dirs( inDir )
Dir.foreach( inDir ) {|x|
print( x )
if File.ftype( x ) == “directory”
list_dirs( x )
end
}
end

ARGV.each{ |dirName|
list_dirs(dirName)
}

Dir.foreach lists “.” (current directory" and “…” (parent directory)
which
is why you are getting infinite recursion. Try adding:
next if x == “.” or x == “…”

To make your program work you will also need to chdir() into each
directory,
or else build up a pathname, so that directories within directories are
found. Something like this:

path = inDir + “/” + x
if File.ftype( path ) == “directory”
list_dirs( path )
end

Regards,

Brian.

Thanks Gennady, Martin & Brian! I’m back on track now…

Krishna

···

-----Original Message-----
From: Gennady [mailto:gfb@tonesoft.com]
Sent: Tuesday, March 25, 2003 1:15 PM
To: ruby-talk ML
Subject: Re: Recursive methods allowed in Ruby?

For this particular case, it is much easier to use class Find (require
“find”, p. 450 of Pickaxe) to do directory traversing for you. It works
similar to Unix ‘find’ command.

Gennady.

----- Original Message -----
From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, March 25, 2003 1:01 PM
Subject: Re: Recursive methods allowed in Ruby?

On Wed, Mar 26, 2003 at 05:50:47AM +0900, Krishna Dole wrote:

def list_dirs( inDir )
Dir.foreach( inDir ) {|x|
print( x )
if File.ftype( x ) == “directory”
list_dirs( x )
end
}
end

ARGV.each{ |dirName|
list_dirs(dirName)
}

Dir.foreach lists “.” (current directory" and “…” (parent directory)
which
is why you are getting infinite recursion. Try adding:
next if x == “.” or x == “…”

To make your program work you will also need to chdir() into each
directory,
or else build up a pathname, so that directories within directories are
found. Something like this:

path = inDir + “/” + x
if File.ftype( path ) == “directory”
list_dirs( path )
end

Regards,

Brian.