Updated April 10, 2023
Introduction to Ruby Block
Block in Ruby is a similar concept like any method. Ruby block allows us to use all calculations and manipulation in the same way how we perform inside any method. So we can say blocks in Ruby are similar to any method, but blocks does not belongs to any objects, broadly we can say that there could be three types of block in Ruby first block with do and end, block with inline (basically a block with curly braces ) and a block which can get some argument with help of the pipe symbol(||), all these types of blocks will be used to do some calculation and manipulation (addition, changing data or any arithmetic operations).
Syntax of ruby block:
We can divide the syntax for blocks in Ruby in three sections.
- Writing block within the do and end statement. In the below syntax we have created a block with the name name_of_block, here this block contains some code statement which will be executed.
name_of_block do
#code statement 1
#code statement 2
#code statement 3
…
...
end
- Writing block with help of curly braces. In this syntax block we have created a block with name name_of_block and within this we have created a block with curly braces which contains some codes for executions.
name_of_block{ #list of statement which is going to be executed }
- A block where we can also pass some arguments. To pass argument to block we can use the || symbol which allows us to pass the argument blocks.
How do Blocks work in Ruby?
Ruby Block plays a very vital role in handling various manipulations and it is a substitute for methods in Ruby. Many people are not comfortable for writing block in Ruby they preferred to write method instead of writing block. With help of block code we will be able to reduce the code size.
Given below is the working of the block in the following steps:
- Any block is similar to the closure of other programming languages.
- Biggest advantage of using block is it can get any number of arguments and return a value.
- Block also performs tasks in the same way how methods work. For example in block we can do addition and any arithmetic operations.
- Every block is made of code chunks, which means it contains the chunks of the codes which are performing a separate activity.
- An important thing about the block is it will always call by any method or we can pass it to any other method.
- In case if we wanted to use the block inside any method then we should use yield keyword.
- If we want we can make a call to the block inside any method, here block is passed to method while calling a method.
In Ruby we can have three different types:
- With help of the do and end statement: This is the most common approach to create any block in Ruby. We can write some repetition code with help of do and get each attribute of it.
- Block with curly braces or by using inline blocks: We can use the curly braces for creating any block in Ruby, inside the curly braces we can write the codes or codes for compilation of certain task blocks.
- Block with arguments: We can pass some arguments to the block with help of the || symbol .
Examples of Ruby Block
Given below are the examples mentioned:
Example #1
Below is an example to create a block with the do and end statement. Here in the below example we have taken some user list of arrays and we are running each on them. Each will go through each element of the user list and we have mention the do block which will keep getting each name. Inside this block we are printing the users one by one from the list of thee users. You can write some logic also instead of printing the value of each user.
Code:
["Ranjan", "Ajay", "Vijay","Suresh"].each do |name|
puts name
end
Output:
Example #2
Here we are using the concept of the inline or curly braces statement logic. We can see in the below example we have taken some user lists and we have used each method to repeat for printing every element of the user list. Inside the curly braces we can put some logic instead of printing the user list .
Code:
["Ranjan", "Ajay", "Vijay","Suresh"].each {|name| puts name}
Output:
Example #3
Below is an example where we are passing the argument to the blocks.
Code:
students = ["Ranjan", "Ajay", "Vijay","Suresh","Anjali","Alka","Priya","Vivek","Suresh","Nikita","Krishna","Jaya"]
students.each do |students|
puts students
end
Output:
Example #4
Here we are using the concept of the inline or curly braces statement logic. We can see in the below example we have taken some number lists and we have used a select method to repeat for finding odd numbers out of the given list of the numbers. Every element of the number list will be traversed and check if it is odd or not. Inside the curly braces we can put some logic instead of printing the odd numbers.
Code:
puts [22, 21, 19, 11, 51,31].select { |number| number.odd? }
Output:
Conclusion
Here we have seen the basic concept of the blocks in the ruby, we saw the working of the blocks and its various types of syntax and importance of them in the real world. We saw ruby blocks with some practical and useful examples.
Recommended Articles
We hope that this EDUCBA information on “Ruby Block” was beneficial to you. You can view EDUCBA’s recommended articles for more information.