Hi!
In german ruby-forum we had a question which ends up in unclear situations. The following code:
----- Code -----
def useblk1
puts "block (useblk1) returns '#{yield}'"
99
end
puts "useblk1 returns '#{useblk1 {break 42}}'"
def useblk2(&blk)
puts "block (useblk2) returns '#{blk.call}'"
99
end
puts "useblk2 returns '#{useblk2 {break 42}}'"
def useblk3
puts "block (useblk3) returns '#{yield}'"
99
end
puts "useblk3 returns '#{useblk3(&lambda{break 42})}'" rescue puts "useblk3 error: #$!"
def useblk4
puts "block (useblk4) returns '#{yield}'"
99
end
puts "useblk4 returns '#{useblk4(&Proc.new{break 42})}'" rescue puts "useblk4 error: #$!"
----- End of Code -----
results under Ruby 1.8.6 (OneClickInstaller-186-25) in:
----- Result Ruby 1.8.6 -----
useblk1 returns '42'
useblk2 returns '42'
useblk3 error: break from proc-closure
useblk4 error: break from proc-closure
----- End of Result -----
Which surprises me. When I read "PickAxe2" all should return from the method with the value presented by "break".
When I run this Program with Ruby 1.9 (MSYS/MinGW build on Windows2000, 2007-05-15 patchlevel 0) the result is the following:
----- Result Ruby 1.9 -----
useblk1 returns '42'
block (useblk2) returns '42'
useblk2 returns '99'
useblk3 returns '42'
useblk4 returns '42'
----- End of Result -----
Here I'm confused by the result of "useblk2", but the rest is clear for me.
Did I make an error? - Are there Errors in 1.8.6? - Are there Errors in 1.9? - ???
I must admit that I am confused...
Wolfgang Nádasi-Donner