My TIIDELab Experience 1.0

The last month has been a pleasurable experience for myself and 59 other privileged fellows. TIIDELab and her sponsors have made it so. They have set us on a 5-month journey into software…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Final thoughts about suffixes in Ruby

In this article, we will consider ending thoughts about how suffixes are used in ruby methods.

This article is divided into the following sections:

The use of suffixes is a convention in ruby, which means that you can not use them. However, it will be great if use them. It will give more expressiveness to your code and as a result, other programmers will spend less time investigating your code.

For example there are methods like pop and shift. They don’t have the exclamation mark at the end of the method name, however, when we use them — we mutate the object.

Method pop:

produces:

As you can see, the array was changed. First, we created an array with 3 elements, and after we used method pop we got the array with only 2 elements in it. The fact that we end up having the same object is confirmed by the fact that it has the same object id that was in the beginning. According to conventions for methods naming , the pop method should have the exclamation mark at the end of the method name, but it doesn’t.

Another example, the shift method:

produces:

As we see, we have the same identity here. Thus, you should be careful when using the standard library methods, if the method doesn’t have the exclamation mark at the end of the method name, this doesn’t mean that it’s not a mutator method. The same conclusion is true for methods with other suffixes. In most cases, the methods will satisfy the requirements of the conventions, but it’s better to check first and then use a method more properly. A good practice is to take a look at the documentation and test the method through irb, then you will definitely know what the method does.

It’s not a secret that different libraries were written by different programmers and who knows how they used the suffixes. In some libraries (for example, in rails) there are can be methods with the exclamation mark which can has a different meaning (for example, the beng method can tell if we will see an error message after using this method). Thus, as in the case of the standard ruby library, methods should be checked.

— use of suffixes allows you to write better code
— don’t trust usages of suffixes in ruby libraries (even when you work with standard ruby library) — check what you use

Add a comment

Related posts:

The Roof is Leaking

Oh DANG! The roof is leaking again… Can you fix it before dinner, Honey???. “The Roof is Leaking” is published by Mal in SNAPSHOTS.

Engineering Internships at Redbubble

We revamped the Redbubble internship program to give our interns the best possible experience. To learn a lot, work with one of our cross-functional teams and get the support they need.

Chimpzee

A new economic model in the gaming industry where players can earn real-world value by playing a game. Players are incentivized with crypto-based rewards for their time and effort, and these rewards…