A method can then invoke an associated block one or more time using the Ruby yield statement. Thus any method that wants to take a block as a parameter can use the yield keyword to execute the block at any time.
Program p022codeblock.rb illustrates what we have just discussed.
- =begin
- Ruby Code blocks are chunks of code between braces or
- between do- end that you can associate with method invocations
- =end
- def call_block
- puts 'Start of method'
- # you can call the block using the yield keyword
- yield
- yield
- puts 'End of method'
- end
- # Code blocks may appear only in the source adjacent to a method call
- call_block {puts 'In the block'}
The output is:
- >ruby p022codeblock.rb
- Start of method
- In the block
- In the block
- End of method
- >Exit code: 0
If you provide a code block when you call a method, then inside the method, you can yield control to that code block - suspend execution of the method; execute the code in the block; and return control to the method body, right after the call to yield. If no code block is passed, Ruby raises an exception:
- no block given (LocalJumpError)
You can provide parameters to the call to yield: these will be passed to the block. Within the block, you list the names of the arguments to receive the parameters between vertical bars (|).
The program p023codeblock2.rb illustrates the same.
- # You can provide parameters to the call to yield:
- # these will be passed to the block
- def call_block
- yield('hello', 99)
- end
- call_block {|str, num| puts str + ' ' + num.to_s}
The output is:
- >ruby p023codeblock2.rb
- hello 99
- >Exit code: 0
Note that the code in the block is not executed at the time it is encountered by the Ruby interpreter. Instead, Ruby remembers the context in which the block appears and then enters the method.
A code block's return value (like that of a method) is the value of the last expression evaluated in the code block. This return value is made available inside the method; it comes through as the return value of yield.
block_given? returns true if yield would execute a block in the current context. Refer to the following example:
- def try
- if block_given?
- yield
- else
- puts "no block"
- end
- end
- try # => "no block"
- try { puts "hello" } # => "hello"
- try do puts "hello" end # => "hello"
Block Variables
Let us see what happens in the following example when a variable outside a block is x and a block parameter is also named x.
- x = 10
- 5.times do |x|
- puts "x inside the block: #{x}"
- end
- puts "x outside the block: #{x}"
The output is:
- x inside the block: 0
- x inside the block: 1
- x inside the block: 2
- x inside the block: 3
- x inside the block: 4
- x outside the block: 10
You will observe that after the block has executed, x outside the block is the original x. Hence the block parameter x was local to the block.
Next observe what happens to x in the following example:
- x = 10
- 5.times do |y|
- x = y
- puts "x inside the block: #{x}"
- end
- puts "x outside the block: #{x}"
The output is:
- x inside the block: 0
- x inside the block: 1
- x inside the block: 2
- x inside the block: 3
- x inside the block: 4
- x outside the block: 4
Since x is not a block parameter here, the variable x is the same inside and outside the block.
In Ruby 1.9, blocks introduce their own scope for the block parameters only. This is illustrated by the following example:
- x = 10
- 5.times do |y; x|
- x = y
- puts "x inside the block: #{x}"
- end
- puts "x outside the block: #{x}"
The output is:
- x inside the block: 0
- x inside the block: 1
- x inside the block: 2
- x inside the block: 3
- x inside the block: 4
- x outside the block: 10