def list base_dir
d= Dir.new base_dir
d.each {|x|
return “asd” if x.eql? “.”
return “asd” if x.eql? “…”
puts “Got #{x}”
full_name = base_dir+"/"+x
if(File.directory?(full_name))
list full_name
end
}
end
puts ( list base_dir)
The return inside the block in the “list” method actually return from
the “list” method itself. But i just wanted to exit the block. how does
one exit from a block in ruby?
Why do you use “puts” here? As far as I can see you do the printing in #list.
The return inside the block in the “list” method actually return from
the “list” method itself. But i just wanted to exit the block. how does
one exit from a block in ruby?
def list base_dir, prefix
Dir.foreach(base_dir){|file|
if (file.eql? “.” or file.eql? “…”)
puts “break dosent work either”
break
end
# this if is the only solution i found, but its ugly.
if !(file.eql? "." or file.eql? "..")
puts "Got #{prefix+file.gsub(/.java$/,"")}" if file =~ /.*\.java$/
full_name = base_dir+"/"+file
if(File.directory?(full_name))
list full_name, prefix+file+"."
end
end
}
end
list “c:/java/”, “”
break not only exited the block but also prevented execution with the
next file in the directory, break actually broke the foreach loop
entirely, I just wanted something like continue
please advise.
I am trying to recurse all directories and find certain type of files.
Stefano C. wrote:
Alle martedì 13 novembre 2007, Jigar G. ha scritto:
list full_name
please advise.
You should use break instead of return.
next file in the directory, break actually broke the foreach loop
entirely, I just wanted something like continue
please advise.
That would be “next”. Note that exiting from a block and switching to
the next iteration is something completely different. You asked for
exiting and thusly were correctly referred to “break”.
next file in the directory, break actually broke the foreach loop
entirely, I just wanted something like continue
please advise.
That would be “next”. Note that exiting from a block and switching to
the next iteration is something completely different. You asked for
exiting and thusly were correctly referred to “break”.