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
["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
["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
india_states = ["Andhra Pradesh", "Assam", "Bihar", "Chhattisgarh",
"Goa", "Gujarat", "Haryana", "Arunachal Pradesh",
"Karnataka", "Manipur", "Punjab", "Uttar Pradesh",
"Uttarakhand"]
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
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
def shivi
puts "Inside Method !"
yield
puts "Again Inside Method !"
yield
end
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
def shivi
puts "Inside Method !"
yield "p1"
puts "Again Inside Method !"
yield "p2"
end
shivi{ |para| puts "Inside Block
|
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
BEGIN {
puts "This is BEGIN block Code"
}
END {
puts "This is END block code"
}
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
x = "Outside the block"
4 .times do |x|
puts "Value Inside the block:
end
puts "Value Outside the block:
|
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Aug, 2022
Like Article
Save Article