# How do I exit from a block

**URL:** <https://rubytalk.org/t/how-do-i-exit-from-a-block/42161>\
**Category:** ruby-talk\
**Created:** [13 November 2007 14:11 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161 "2007-11-13T14:11:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jigar\_Gosar](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jigar\_Gosar](https://rubytalk.org/u/Jigar_Gosar)\
**Post date:** [13 November 2007 14:11 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/1 "2007-11-13T14:11:21Z")

</div>

base\_dir = "C:/pde"

def list base\_dir  
&nbsp;&nbsp;d= Dir.new base\_dir  
&nbsp;&nbsp;d.each {|x|  
&nbsp;&nbsp;&nbsp;&nbsp;return "asd" if x.eql? "."  
&nbsp;&nbsp;&nbsp;&nbsp;return "asd" if x.eql? ".."  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{x}"  
&nbsp;&nbsp;&nbsp;&nbsp;full\_name = base\_dir+"/"+x  
&nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(full\_name))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list full\_name  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}  
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-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [13 November 2007 14:16 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/2 "2007-11-13T14:16:37Z")

</div>

Alle martedì 13 novembre 2007, Jigar Gosar ha scritto:

> base\_dir = "C:/pde"
> 
> def list base\_dir  
> &nbsp;&nbsp;d= Dir.new base\_dir  
> &nbsp;&nbsp;d.each {|x|  
> &nbsp;&nbsp;&nbsp;&nbsp;return "asd" if x.eql? "."  
> &nbsp;&nbsp;&nbsp;&nbsp;return "asd" if x.eql? ".."  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{x}"  
> &nbsp;&nbsp;&nbsp;&nbsp;full\_name = base\_dir+"/"+x  
> &nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(full\_name))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list full\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> 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.

Stefano

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 November 2007 14:23 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/3 "2007-11-13T14:23:00Z")

</div>

> base\_dir = "C:/pde"
> 
> def list base\_dir  
> &nbsp;&nbsp;d= Dir.new base\_dir  
> &nbsp;&nbsp;d.each {|x|  
> &nbsp;&nbsp;&nbsp;&nbsp;return "asd" if x.eql? "."  
> &nbsp;&nbsp;&nbsp;&nbsp;return "asd" if x.eql? ".."  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{x}"  
> &nbsp;&nbsp;&nbsp;&nbsp;full\_name = base\_dir+"/"+x  
> &nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(full\_name))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list full\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;}  
> 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

> **···**
>
> 2007/11/13, Jigar Gosar \<jigar.gosar@gmail.com\>:
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Dam](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dam](https://rubytalk.org/u/Dam)\
**Post date:** [13 November 2007 14:41 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/4 "2007-11-13T14:41:10Z")

</div>

Or even

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

see [class Dir - RDoc Documentation](http://www.ruby-doc.org/core/classes/Dir.html#M002348)

Regards,

> **···**
>
> 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
> 
> --  
> Damien MERENNE \<dam@cosinux.org\>  
> [http://blog.cosinux.org/](http://blog.cosinux.org/)

---

<div class="post-metadata">

**Author:** ![Jigar\_Gosar](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jigar\_Gosar](https://rubytalk.org/u/Jigar_Gosar)\
**Post date:** [13 November 2007 14:41 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/5 "2007-11-13T14:41:15Z")

</div>

def list base\_dir, prefix  
&nbsp;&nbsp;Dir.foreach(base\_dir){|file|  
&nbsp;&nbsp;&nbsp;&nbsp;if (file.eql? "." or file.eql? "..")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "break dosent work either"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;# this if is the only solution i found, but its ugly.  
&nbsp;&nbsp;&nbsp;&nbsp;if !(file.eql? "." or file.eql? "..")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{prefix+file.gsub(/.java$/,"")}" if file =~ /.\*\.java$/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;full\_name = base\_dir+"/"+file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(full\_name))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list full\_name, prefix+file+"."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;}  
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:
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list full\_name  
> > > please advise.
> > 
> > You should use break instead of return.
> > 
> > Stefano
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jigar\_Gosar](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jigar\_Gosar](https://rubytalk.org/u/Jigar_Gosar)\
**Post date:** [13 November 2007 14:51 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/6 "2007-11-13T14:51:58Z")

</div>

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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jigar\_Gosar](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jigar\_Gosar](https://rubytalk.org/u/Jigar_Gosar)\
**Post date:** [13 November 2007 15:03 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/7 "2007-11-13T15:03:42Z")

</div>

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|  
&nbsp;&nbsp;file = f.gsub(/.\*\/src\/core\//,"")  
&nbsp;&nbsp;file = file.gsub("/",".")  
&nbsp;&nbsp;file = file.gsub(/.java$/,"")  
&nbsp;&nbsp;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 [class Dir - RDoc Documentation](http://www.ruby-doc.org/core/classes/Dir.html#M002348)
> > 
> > Regards,
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [13 November 2007 15:05 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/8 "2007-11-13T15:05:34Z")

</div>

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

> **···**
>
> 2007/11/13, Jigar Gosar \<jigar.gosar@gmail.com\>:
> 
> > def list base\_dir, prefix  
> > &nbsp;&nbsp;Dir.foreach(base\_dir){|file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;if (file.eql? "." or file.eql? "..")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "break dosent work either"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# this if is the only solution i found, but its ugly.  
> > &nbsp;&nbsp;&nbsp;&nbsp;if !(file.eql? "." or file.eql? "..")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{prefix+file.gsub(/.java$/,"")}" if file =~ /.\*\.java$/  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;full\_name = base\_dir+"/"+file  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(full\_name))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list full\_name, prefix+file+"."  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;}  
> > 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.
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Jigar\_Gosar](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@Jigar\_Gosar](https://rubytalk.org/u/Jigar_Gosar)\
**Post date:** [13 November 2007 15:16 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/9 "2007-11-13T15:16:37Z")

</div>

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\>:
> > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dam](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dam](https://rubytalk.org/u/Dam)\
**Post date:** [13 November 2007 15:31 UTC](https://rubytalk.org/t/how-do-i-exit-from-a-block/42161/10 "2007-11-13T15:31:54Z")

</div>

If you want only the name of the file, use

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

Best,

> **···**
>
> 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|  
> > &nbsp;&nbsp;file = f.gsub(/.\*\/src\/core\//,"")  
> > &nbsp;&nbsp;file = file.gsub("/",".")  
> > &nbsp;&nbsp;file = file.gsub(/.java$/,"")  
> > &nbsp;&nbsp;puts file  
> > }
> > 
> > Any way to shorten it further??
> 
> --  
> Damien MERENNE \<dam@cosinux.org\>  
> [http://blog.cosinux.org/](http://blog.cosinux.org/)
