Ruby Blocks

by

in

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:

block_name{  

   statement1  

   statement2  

   ..........  

}

Example:

The below example shows the multi-line block.


  1. [10, 20, 30].each do |n|   
  2.  puts n   
  3. end

Output:

Ruby Blocks 1

Below example shows the inline block.

[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:

#!/usr/bin/ruby   

  

def met   

   puts "This is method"   

   yield   

   puts "You will be back to method"   

   yield   

end   

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

Here, the block variable inside method met is a reference to the block. It is executed with the call mehtod. The call method is same as yield method.


Initializing objects with default values

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

Example:

class Novel   

  attr_accessor :pages, :category   

  

  def initialize   

    yield(self)   

  end   

end   

  

novel = Novel.new do |n|   

  n.pages = 564   

  n.category = "thriller"   

end   

  

puts "I am reading a #{novel.category} novel which has #{novel.pages} pages."

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *