The break statement for blocks (according to the Ruby programming language ) is defined as follows:
it forces the block to return to its iterator and iterator in order to return to the method that called it.
Therefore, when the following code is executed, this results in a LocalJumpError error.
def test puts "entering test method" proc = Proc.new { puts "entering proc"; break } proc.call # LocalJumpError: iterator has already returned puts "exiting test method" end test
While the following code does not execute , a LocalJumpError is raised. What is special about the ampersand? Does the ampersand sign implicitly use Proc.new?
def iterator(&proc) puts "entering iterator" proc.call
In other words, I am reading the ampersand sign as a means of connecting the call to Proc.new. In this case, the behavior should be the same as the first piece of code.
def iterator (p = Proc.new { puts "entering proc"; break}) ... end
Disclaimer I am learning the language (ruby 1.9.2) and therefore I will be grateful for the links and a detailed review.
Salman paracha
source share