Simplify Ruby

Tram Ho

One of Ruby’s most unique and often misunderstood features is blocks. Blocks are closed versions of Ruby and can be used to make code more reusable and less verbose. But keywords like yield can be confusing at first and make this function a bit scary at work. This article aims to go over the essentials and build your knowledge in small pieces.

Block – What is a block?

Blocks are anonymous functions whose return value will be applied to the method that calls it. It is a somewhat confusing sentence so work harder to understand.

Blocks consist of code between a set of {} signs or a do/end pair. The former is the definition of a line and the latter is the definition of multiple lines.

Defines a line that is primarily used for a class. For consistency, I used the do / end syntax in all the examples.

If you’ve worked with Ruby before, I almost guarantee that you’ve ever seen a block. The each and map methods are two of the most commonly used iterators for block usage.

How do you create a block?

If we want to create a simple function that prints the input wrapped by ⭐️, we can write a simple code like this:

If we want to rewrite this function using block notation, we can do the following:

As shown above, the return value of the block content is what is passed to the keyword yield in the function.

We can also make the block more customizable by passing a parameter to the function we created.

If we want to reference the values ​​from our function in the attached block, then we can pass the yield argument and reference it in the block parameters …

But why use blocks on a common function?

So far, it seems that we don’t do anything that a typical method can’t do. Blocks are useful when we want to apply reusable logic to different contexts in a compact way.

For example, suppose we know that we always want to print the output of a series of commands between a “⭐️⭐️⭐️” set. We can use blocks to apply packet logic to different contexts. without having to perform auxiliary functions.

As shown above, stars are always printed before and after the code is executed in a block. Although 🧡 is found quite different from 💜, ️ star_wrap this method allows us to apply package logic to both contexts in a discreet manner.

Error handling block

Any method can accept a block, even if it is not referenced in the function. The block contents will do nothing.

We can call all the blocks in the examples above because we used the yield keyword. So, what if we call yield and do not provide a block? An error will be raised.

Can we fix this problem using block_given? Expressions for checking block usage.

Pass a block as a parameter

If we want to be more explicit about calling a block and having a reference to it, we can pass it into a method as a parameter.

In this case, the block is turned into a Proc object that we can call with .call . Using blocks in this way is useful when you want to pass blocks over functions. We specify the block parameter by passing it as the last argument and adding that & .

Below, the star_wrap_a and star_wrap_b methods do the exact same thing …

Block in fact

In the default Rails application.html.erb , application.html.erb is loaded for every page with the driver inheriting from that ApplicationController . If a child controller of ApplicationController shows a view, its content is brought to application.html.erb . With this function, boilerplate HTML must be applied to all application pages that can be easily implemented.

And those are necessary!

Blocks do not have to contain complex logic for us to use. They are often used to abstract shared logic that can be applied to countless contexts. If written carefully, code can be created more neatly and more easily read by exploiting their functions. To reinforce any of the concepts mentioned above, I recommend that you try the code samples in the interactive panel or by writing your own examples. Good luck!

References

https://medium.com/gusto-engineering/ruby-blocks-simplified-397e56d63259

Share the news now

Source : Viblo