Hello,
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.
Ferenc
Simon Strandgaard 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?
ruby a.rb
before
after result=42
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 }
–
Simon Strandgaard
By way of explanation, I believe the difference is this: when the block
is invoked implicitly (via ‘yield’) a ‘return’ or ‘break’ will affect a
scope greater than itself (not sure if my terminology is correct here).
When the block is invoked explicitly (via the ‘call’ method of Proc),
then ‘return’ and ‘break’ affect only the block itself.
Perhaps some Ruby guru could verify whether this is accurate or not.
This seems to be the behavior as I’ve seen it, at least according to my
few forays into the Ruby source code.
···
On Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote:
–
Jamis Buck
jgb3@email.byu.edu
ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’
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
ruby a.rb
before
after result=42
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 }
–
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 }
···
On Wed, 21 Jan 2004, Simon Strandgaard wrote:
On Wed, 21 Jan 2004 05:37:48 +0900, Ferenc Engard wrote:
–
Charlie
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).
next value
Thanks, this is it. The documentation should be updated!
Ferenc
How about this code?
I am using cygwin on XP and I get the following :
$ ./a.rb
before
Wierd… is this a bug? am I doing something wrong?
ruby -v
ruby 1.8.1 (2003-12-22) [i386-freebsd5.1]
···
On Wed, 21 Jan 2004 08:03:33 +0900, Charles Mills wrote:
On Wed, 21 Jan 2004, Simon Strandgaard wrote:
–
Simon Strandgaard
Here is my version info:
$ ruby -v
ruby 1.8.0 (2003-08-04) [i386-cygwin]
I am not sure how ruby is suppost to behave in this instance.
···
On Wed, 21 Jan 2004, Simon Strandgaard wrote:
On Wed, 21 Jan 2004 08:03:33 +0900, Charles Mills wrote:
On Wed, 21 Jan 2004, Simon Strandgaard wrote:
How about this code?
I am using cygwin on XP and I get the following :
$ ./a.rb
before
Wierd… is this a bug? am I doing something wrong?
ruby -v
ruby 1.8.1 (2003-12-22) [i386-freebsd5.1]
–
Simon Strandgaard
–
Charlie