Ruby Blocks as the name suggests, is actually a block, where a chunk of code lies.
We will be explaining two important types of blocks :
Let us see the Begin and End Block first.
BEGIN { puts "BEGIN block executes" } END { puts "END block executes" } x = 9 y = 5 z = x + y puts "The added value is : #{z}"
Now, if you see the above code, there is block with the name BEGIN.
BEGIN { puts "BEGIN block executes" }
And the BEGIN block starts with a start brace { and ends with a end brace }. All the set of codes you want to write, can be written inside the braces.
Right now, we just have one print statement inside the BEGIN block.
puts "BEGIN block executes"
But we can have as many lines as we want.
Similarly, there is an END block that is defined in the same way the BEGIN block is defined.
END { puts "END block executes" }
Then we have written our usual way to add two numbers and print them.
x = 9 y = 5 z = x + y puts "The added value is : #{z}"
Now, if you see the output.
BEGIN block executes The added value is : 14 END block executes
You can see that the BEGIN block executes first. Then the code to add two numbers. And finally the END block executes.
So, if you need a code that has to be executed before your normal lines of code begins. You can put them in the BEGIN block.
Similarly, if you want to put some code that needs to execute once your program ends. You can put them in the END block.
Let us look at the second way of defining a block where the block has a name and can be called using yield statement.
So far, we have seen that a method has a name and can be called using its name.
A block with a name is something of that kind.
Now, we will be seeing the usage of a method that is quite different.
Let us see with the below example.
def mymethod() puts "Calling the block from the method" yield puts "Back to the method" end mymethod { puts "Inside the block" }
Now, if you see the above code, we can see that there is a method named mymethod().
def mymethod() puts "Calling the block from the method" yield puts "Back to the method" end
Everything seems common in the method except the yield.
The yield statement is used to call the block that is defined below.
mymethod { puts "Inside the block" }
But! Just think.
How would the yield statement which block to call?
Well! The name of the block must be of the same name as that of the method name.
If you see the above code, the method name and block name is just the same i.e. mymethod.
But! Once again, don't you notice something weird?
The method mymethod() never got called. Then how did it execute?
Well! It happens in the case of blocks.
You can also pass values from the method to the block.
Let us modify the above example.
def mymethod() x = 5 puts "Calling the block from the method" yield x puts "Back to the method" end mymethod { |a| puts "Inside the block" puts "Printing the value of x : #{a}" }
And all we have done is, declared a variable called x and initialised 5 to it.
x = 5
Then we have written the yield statement that calls the mymethod block, passing the value of x.
yield x
Now, if we come to the mymethod block, we can see that it has variable a inside ||
mymethod { |a|
To accept the value of x.
Now, the value of x is passed to the variable a inside the block.
mymethod { |a| puts "Inside the block" puts "Printing the value of x : #{a}" }
And finally, we print the value of a in the mymethod block.