Ruby Blocks

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Blocks

Ruby code blocks are called closures in other programming languages. It consist of a group of codes which is always enclosed with braces or written between do..end. The braces syntax always have the higher precedence over the do..end syntax. Braces have high precedence and do has low precedence.

A block is written in two ways,

  • Multi-line between do and end (multi-line blocks are niot inline)
  • Inline between braces {}

Both are same and have the same functionality.

To invoke a block, you need to have a function with the same name as the block.

A block is always invoked with a function. Blocks can have their own arguments.

syntax:

  1. block_name{  
  2.    statement1  
  3.    statement2  
  4.    ..........  
  5. }  

Example:

The below example shows the multi-line block.

 

Output:

  Ruby Blocks 1

Below example shows the inline block.

  1. [10, 20, 30].each {|n| puts n}  

Output:

Ruby Blocks 2

The yield statement

The yield statement is used to call a block within a method with a value.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. def met   
  4.    puts "This is method"   
  5.    yield   
  6.    puts "You will be back to method"   
  7.    yield   
  8. end   
  9. met {puts "This is block"

Output:

Ruby Blocks 3

While the execution of met method, when we reach at yield line, the code inside the block is executed. When block execution finishes, code for met method continues.

Passing parameters with yield statement

One or more than one parameter can be passed with the yield statement.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. def met   
  4.    yield 1   
  5.    puts "This is method"   
  6.    yield 2   
  7. end   
  8. met {|i| puts "This is block #{i}"

Output:

Ruby Blocks 4

Block Variables

We can use same variable outside and inside a block parameter. Let's see the following example.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3.     x = "Outer variable"    
  4.     3.times do |x|    
  5.       puts "Inside the block: #{x}"    
  6.     end    
  7.     puts "Outside the block: #{x}"  

Output:

Ruby Blocks 5

In this example, we are using same variable inside the block as the block parameter x and outside the block as a variable x.

BEGIN and END block

Ruby BEGIN and END block is used to declare that file is being loaded and file has been loaded respectively.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. BEGIN {   
  4.   puts "code block is being loaded"   
  5. }   
  6.   
  7. END {   
  8.   puts "code block has been loaded"   
  9. }   
  10. puts "This is the code block"  

Output:

Ruby Blocks 6

Ampersand parameter (&block)

The &block is a way to pass a reference (instead of a local variable) to the block to a method.

Here, block word after the & is just a name for the reference, any other name can be used instead of this.

Example:

  1. def met(&block)   
  2.   puts "This is method"   
  3.   block.call   
  4. end   
  5. met { puts "This is &block example" }   

Output:

Ruby Blocks 7

Initializing objects with default values

Ruby has an initializer called yield(self). Here, self is the object being initialized.

Example:

  1. class Novel   
  2.   attr_accessor :pages, :category   
  3.   
  4.   def initialize   
  5.     yield(self)   
  6.   end   
  7. end   
  8.   
  9. novel = Novel.new do |n|   
  10.   n.pages = 564   
  11.   n.category = "thriller"   
  12. end   
  13.   
  14. puts "I am reading a #{novel.category} novel which has #{novel.pages} pages."   

 

Output:

Ruby Blocks 8