From: Charles Mills Date: 2004-01-21T08:03:33+09:00 Subject: Re: question about blocks On Wed, 21 Jan 2004, Simon Strandgaard wrote: > On Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote: > > How can I return a value from a block? "return" immediately returns from > > the caller method, "break" bypasses the called method (which yielded the > > block). > > > > I have read something about this, but cannot remember where. I know the > > syntax changed between 1.6.8 and 1.8. > > How about this code? > I am using cygwin on XP and I get the following : $ ./a.rb before > > server> ruby a.rb > before > after result=42 > server> expand -t2 a.rb > def test(&block) > puts "before" > res = nil > begin > res = block.call > rescue LocalJumpError => e > res = e.exit_value > end > puts "after result=#{res}" > end > > test { return 42 } > server> > > -- > Simon Strandgaard > > here is the contents of my a.rb: #!/usr/bin/ruby def test(&block) puts "before" res = nil begin res = block.call rescue LocalJumpError => e res = e.exit_value end puts "after result=#{res}" end test { return 42 } -- Charlie