![]() |
VOOZH | about |
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:
A block code can be used in two ways as follows:
block_name do #statement-1 #statement-2 . . end
Example:
Output:.
Geeks GFG 55
block_name { #statements_to_be_executed }
Example:
Output:.
Geeks GFG 55
Block Arguments: Arguments can be passed to block by enclosing between the pipes or vertical bars( | | ).
Example:
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:
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 is used to call a block inside the method using the yield keyword with a value.
Example:
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:
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:
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:
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