[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I exit from a block

Jigar Gosar

11/13/2007 2:11:00 PM

base_dir = "C:/pde"

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?

please advise.
--
Posted via http://www.ruby-....

9 Answers

Stefano Crocco

11/13/2007 2:17:00 PM

0

Alle marted=C3=AC 13 novembre 2007, Jigar Gosar ha scritto:
> base_dir =3D "C:/pde"
>
> def list base_dir
> d=3D Dir.new base_dir
> d.each {|x|
> return "asd" if x.eql? "."
> return "asd" if x.eql? ".."
> puts "Got #{x}"
> full_name =3D 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?
>
> please advise.

You should use break instead of return.=20

Stefano

Robert Klemme

11/13/2007 2:23:00 PM

0

2007/11/13, Jigar Gosar <jigar.gosar@gmail.com>:
> base_dir = "C:/pde"
>
> 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)

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?
>
> please advise.

Not exactly the answer you were looking for, but...

15:21:46 /$ ruby -r find -e 'Find.find("/tmp") {|f| puts f}'
/tmp
/tmp/uscreens
/tmp/uscreens/S-rklemme
/tmp/4052
/tmp/4052/sshlog
/tmp/4052/servicelog
/tmp/4052/runlog
/tmp/4052/keylog
/tmp/.X11-unix
/tmp/.X11-unix/X0

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

dam

11/13/2007 2:41:00 PM

0

On Tue, Nov 13, 2007 at 11:23:00PM +0900, Robert Klemme wrote:
> 2007/11/13, Jigar Gosar <jigar.gosar@gmail.com>:
> > base_dir = "C:/pde"
> >
> > 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
> >
> Not exactly the answer you were looking for, but...
>
> 15:21:46 /$ ruby -r find -e 'Find.find("/tmp") {|f| puts f}'
> /tmp
> /tmp/uscreens
> /tmp/uscreens/S-rklemme
> /tmp/4052
> /tmp/4052/sshlog
> /tmp/4052/servicelog
> /tmp/4052/runlog
> /tmp/4052/keylog
> /tmp/.X11-unix
> /tmp/.X11-unix/X0

Or even

Dir["/tmp/**/*"]

see http://www.ruby-doc.org/core/classes/Dir.ht...

Regards,

--
Damien MERENNE <dam@cosinux.org>
http://blog.co...



Jigar Gosar

11/13/2007 2:41:00 PM

0

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 Crocco wrote:
> Alle martedì 13 novembre 2007, Jigar Gosar ha scritto:
>> list full_name
>> please advise.
> You should use break instead of return.
>
> Stefano

--
Posted via http://www.ruby-....

Jigar Gosar

11/13/2007 2:52:00 PM

0

Robert Klemme wrote:
> 15:21:46 /$ ruby -r find -e 'Find.find("/tmp") {|f| puts f}'
> /tmp
> /tmp/uscreens
> /tmp/uscreens/S-rklemme
> /tmp/4052
> /tmp/4052/sshlog
> /tmp/4052/servicelog
> /tmp/4052/runlog
> /tmp/4052/keylog
> /tmp/.X11-unix
> /tmp/.X11-unix/X0
>
> Cheers
>
> robert


Find look intresing, will solve my problem, thanks.

But I still need a way to exit from a block :)
--
Posted via http://www.ruby-....

Jigar Gosar

11/13/2007 3:04:00 PM

0

Thanks a lot, this is so cool, how did I miss it

my code has reduced to

Dir["C:/pde/cvshome/trunk/OBox/**/src/core/**/*.java"].each{|f|
file = f.gsub(/.*\/src\/core\//,"")
file = file.gsub("/",".")
file = file.gsub(/.java$/,"")
puts file
}

Any way to shorten it further??

Damien Merenne wrote:
> On Tue, Nov 13, 2007 at 11:23:00PM +0900, Robert Klemme wrote:
>> > if(File.directory?(full_name))
>> /tmp/uscreens/S-rklemme
>> /tmp/4052
>> /tmp/4052/sshlog
>> /tmp/4052/servicelog
>> /tmp/4052/runlog
>> /tmp/4052/keylog
>> /tmp/.X11-unix
>> /tmp/.X11-unix/X0
>
> Or even
>
> Dir["/tmp/**/*"]
>
> see http://www.ruby-doc.org/core/classes/Dir.ht...
>
> Regards,

--
Posted via http://www.ruby-....

Robert Klemme

11/13/2007 3:06:00 PM

0

2007/11/13, Jigar Gosar <jigar.gosar@gmail.com>:
> 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.

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".

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

Jigar Gosar

11/13/2007 3:17:00 PM

0

Thanks for clearing that up, I am thoroughly impressed with the ruby
community, best in world. Even professional support is slower.

I am a young convert from java :)

Robert Klemme wrote:
> 2007/11/13, Jigar Gosar <jigar.gosar@gmail.com>:
>> full_name = base_dir+"/"+file
>> 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".
>
> Cheers
>
> robert

--
Posted via http://www.ruby-....

dam

11/13/2007 3:32:00 PM

0

On Wed, Nov 14, 2007 at 12:03:42AM +0900, Jigar Gosar wrote:
> my code has reduced to
>
> Dir["C:/pde/cvshome/trunk/OBox/**/src/core/**/*.java"].each{|f|
> file = f.gsub(/.*\/src\/core\//,"")
> file = file.gsub("/",".")
> file = file.gsub(/.java$/,"")
> puts file
> }
>
> Any way to shorten it further??

If you want only the name of the file, use

puts File.basename(f, ".java")

Best,

--
Damien MERENNE <dam@cosinux.org>
http://blog.co...