Ruby | Blocks
A block is the same thing as a method, but it does not belong to an object. Blocks are called closures in other programming languages. There are some important points about Blocks in Ruby:
- Block can accept arguments and returns a value.
- Block does not have their own name.
- Block consist of chunks of code.
- A block is always invoked with a function or can say passed to a method call.
- To call a block within a method with a value, yield statement is used.
- Blocks can be called just like methods from inside the method that it is passed to.
A block code can be used in two ways as follows:
- Inside the do..end statement Syntax:
block_name do #statement-1 #statement-2 . . end
Example:
Ruby
# Ruby program to demonstrate the block # defined inside do..end statements # here 'each' is the method name # or block name # n is the variable ["Geeks", " GFG ", 55 ]. each do |n| puts n end |
Output:.
Geeks GFG 55
- Inline between the curly braces {} Syntax:
block_name { #statements_to_be_executed }
Example:
Ruby
# Ruby program to demonstrate the block # Inline between the curly braces {} # here 'each' is the method name # n is the variable ["Geeks", " GFG ", 55 ]. each {|i| puts i} |
Output:.
Geeks GFG 55
Block Arguments: Arguments can be passed to block by enclosing between the pipes or vertical bars( | | ).
Example:
Ruby
# Ruby program to demonstrate the # arguments passing to block # here india_states is an array and # it is the argument which is to # be passed to block india_states = ["Andhra Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa", "Gujarat", "Haryana", "Arunachal Pradesh", "Karnataka", "Manipur", "Punjab", "Uttar Pradesh", "Uttarakhand"] # passing argument to block india_states. each do |india_states| puts india_states end |
Output:
Andhra Pradesh Assam Bihar Chhattisgarh Goa Gujarat Haryana Arunachal Pradesh Karnataka Manipur Punjab Uttar Pradesh Uttarakhand
Explanation: In above example, india_states is the argument which is passed to the block. Here it is similar to def method_name (india_states). The only difference is that method has a name but not block and arguments passed between brackets () to method but in block, arguments passed between pipes ||.
How block return values: Actually block returns the value which are returned by the method on which it is called.
Example:
Ruby
# Ruby program to demonstrate how block returns the values # here two methods called i.e 'select' and 'even?' # even? method is called inside the block puts [ 1 , 2 , 3 , 4 , 5 ].select { |num| num.even? } |
Output:
12 14
Explanation: In above example, there are two methods i.e. select , even? and a block. At first, the select method will call the block for each number in the array. First, it will pass the 11 to block, and now inside the block, there is another method named even? which will take it as num variable and return false for 11. This false value will be passed to the select method which will discard it and then it will pass 12 to the block and similarly inside the block, odd? method is called which return true for the 12 and this true value will be passed to select method. Now select method will store this value. Similarly for remaining values in array select method will store the values in the array and at last final array will return to puts method which prints returned array elements on the screen.
The yield Statement
The yield statement is used to call a block inside the method using the yield keyword with a value.
Example:
Ruby
# Ruby program to demonstrate the yield statement # method def shivi # statement of the method to be executed puts "Inside Method !" # using yield statement yield # statement of the method to be executed puts "Again Inside Method !" # using yield statement yield end # block shivi{puts "Inside Block!"} |
Output:
Inside Method! Inside Block! Again Inside Method! Inside Block!
Explanation: In above program, method name is shivi. At first method statements is called which display Inside Method. But as soon as yield statements execute the control goes to block and block will execute its statements. As soon as the block will execute it gives control back to the method and the method will continue to execute from where yield statement called.
Note: Parameters can be passed to the yield statement.
Example:
Ruby
# Ruby program to demonstrate the yield statement # method def shivi # statement of the method to be executed puts "Inside Method !" # using yield statement # p1 is the parameter yield "p1" # statement of the method to be executed puts "Again Inside Method !" # using yield statement # p2 is the parameter yield "p2" end # block shivi{ |para| puts "Inside Block #{para}"} |
Output:
Inside Method! Inside Block p1 Again Inside Method! Inside Block p2
BEGIN and END Block: Ruby source file has a feature to declare the block of code which can run as the file is being loaded i.e the BEGIN block. After the complete execution of the program END block will execute. A program can contain more than 1 BEGIN and END block. BEGIN blocks will always execute in a order but END blocks will execute in reverse order.
Example:
Ruby
# Ruby program to demonstrate the BEGIN and END block #!/usr/bin/ruby # BEGIN block BEGIN { # BEGIN block code puts "This is BEGIN block Code" } # END block END { # END block code puts "This is END block code" } # Code will execute before END block puts "Before END block" |
Output:
This is BEGIN block Code Before END block This is END block code
Note: We can use same variable outside and inside a block.
Example:
Ruby
# Ruby program to demonstrate the use of # same variable outside and inside a block #!/usr/bin/ruby # variable 'x' outside the block x = "Outside the block" # here x is inside the block 4 .times do |x| puts "Value Inside the block: #{x}" end puts "Value Outside the block: #{x}" |
Output:
Value Inside the block: 0 Value Inside the block: 1 Value Inside the block: 2 Value Inside the block: 3 Value Outside the block: Outside the block
Please Login to comment...