Home » Ruby programming

Ruby Blocks

Ruby blocks: In this tutorial, we are going to learn about the blocks in the ruby programming language, their implementations, yield statements, etc.
Submitted by Hrithik Chandra Prasad, on July 28, 2019

Ruby blocks

A block works just like a method but it doesn't have a name. It does not belong to any object. Blocks can also be considered as unspecified chunks of code which can consume input in the form of arguments and return some value. Most importantly, blocks can only be created by the means of passing them to a method when the method is invoked and yield statement is used to invoke a block inside a method along with a value.

Consider the following code,

7.times do
  puts "Oh, hello from inside a block. You are at Includehelp!"
end

Output

Oh, hello from inside a block. You are at Includehelp! 
Oh, hello from inside a block. You are at Includehelp! 
Oh, hello from inside a block. You are at Includehelp! 
Oh, hello from inside a block. You are at Includehelp! 
Oh, hello from inside a block. You are at Includehelp! 
Oh, hello from inside a block. You are at Includehelp! 
Oh, hello from inside a block. You are at Includehelp!

You can observe in the above code that times is a method that is defined on numbers. 7.times prints the expression 7 times.

When times method is invoked only a block is passed which is a named chunk of code between do and end statement.

No object is participating with the method times.

Implementation of Blocks

1) Inside do...end statement

It is the most common use of the block. The major portion of the code has this type of implementation.

Syntax:

    name_of_block  do 
        #expression-1  
        #expression-2  
    end

Example:

["C++", "Ruby", 155,"Includehelp","Java","Python",789,889.6].each do |k|    
    puts k    
end   

Output

C++
Ruby
155
Includehelp
Java
Python
789
889.6

2) Between curly braces {} (Inline Implementation)

The above can be executed without do...end statement. The same can be done by putting the expressions inside the curly braces {}.

Syntax:

    name_of_the_block{ #expression_to_be_executed }

Example:

["C++", "Ruby", 155,"Includehelp","Java","Python",789,889.6].each {|k|    
 puts k }

Output

C++
Ruby
155
Includehelp 
Java
Python
789
889.6

3) The yield statement

When you want to call a block inside the method, Ruby provides you yield statement for this purpose. The keyword yield is used when you invoke a block inside the method.

The yield statement can also consume parameters.

Example:

# method Includehelp
    def Includehelp 	
    # expressions to be executed 
    puts "Inside Includehelp!"
    	# yield statement is used 
    	# Tutorial is the parameter 
    	yield "Tutorial"
    
    # expression to be executed 
    puts "Again Inside Includehelp!"
    
    # using yield statement 
    # Tutorial at Includehelp.com is the parameter 
    yield "Tutorial at Includehelp.com"
end

# block 
Includehelp{ |example| puts "Inside Block #{example}"} 

Output

Inside Includehelp!
Inside Block Tutorial
Again Inside Includehelp!
Inside Block Tutorial at Includehelp.com

In the above code, you can observe that firstly, method expression is invoked which displayed Includehelp. After that when the yield statement is found and executed, the pointer goes inside block for the execution of its statement. After successful execution, the pointer goes back to the method will continue to execute from where the yield statement invoked.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.