Learn about the tag method, each_with_index, respond_to ?, squeeze in ruby

Tram Ho

I have a special interest in the Ruby programming language, so it was the first programming language I learned. Ruby is realistic and concise, and highly scalable. Syntax and structure are easy to see, and there are many language support bases. I want to compile an interesting list of good practices I’ve found during my development with Ruby.

Ruby is integrated a lot of methods. Some methods are very useful and often used, while others are rarely used or their purpose is unknown. Below I will list some of the methods between these two extremes.

1. Tap

The definition of tap according to apidock is as follows:

Yields x to the block, and then returns x . The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain

To better understand the tap method we can go to a real-world example:

You want to update the value of a user object, and return the result of that object after the update, we have the following code:

Writing like this, naturally, at the end of the method, it will return a user variable, which is actually not very good while we know that the tap method can return self. So we can fix the following:

2. each_with_index

This method is certainly more common than before but sometimes forgotten. It’s like the basic each method, but it gives you a benefit: index

The each_with_index method not only allows you to access each element in the array, but also provides us with the index of the current element. This is quite useful when you want to ignore index-based elements.

We have a small example of each_with_index:

Although this method repeats many times to handle this situation, it outlines how to use this method. Similar to each_with_index we have Enumerator # with_index

As the above example shows that with_index is not an array method, in order for this method to work we must first convert the array to Enumerator. And to do that, there are support methods like .to_enum, each, .map

There is a small note that each_with_index is equivalent to with_index (0), because they all have an index index starting with 0.

The with_index (x) method when we pass the x parameter, the index will start from the x value, and by default when no more parameters are entered, the index will start from 0.

3. respond_to?

This method will return true if the object responds with the method name, which may be a symbol. We have the following example has a class including methods public, private, protected.

For public methods, we just need to call the following:

For private, protected methods:

We see that with calls like public methods respond_to? returns false. Note that the second parameter of the method respond_to? Very important here, it denotes that respond_to? can find all methods whose scope includes also private methods. We can call respond_to? For the private and protected methods are as follows:

4. squeeze

This is a very simple method on string, it eliminates consecutive overlapping characters in that string.

When not passed in the parameter it will look for all consecutive duplicates in the string including spaces.

When passing a parameter that is a string representing the range of characters in the alphabet, for example, the letters from bn are just searched to eliminate consecutive characters in the bn range, note that here are spaces overlap will not be eliminated.

Passing the “” parameter will only remove consecutive overlapping spaces.

Conclude:

I hope you enjoyed learning about these methods and trying them out in your project – or even simply playing with them in irb or pry to find out how they work.
Reference: https://apidock.com/ruby/Object/tap https://apidock.com/ruby/Enumerator/each_with_index https://apidock.com/ruby/String/squeeze https: // apidock. com / ruby ​​/ Object / respond_to% 3F

Share the news now

Source : Viblo