3 things you must know about Ruby’s bang methods (!)

3 things you must know about Ruby’s bang methods (!)

In this week’s post I am going to talk you about a topic that was asked in my YouTube channel, which is how to use bang methods in Ruby, those we write with a trailing exclamation mark (!). Stay with me if you want to become an expert on this field. Let’s go!

1. It is just a naming convention

To Ruby the exclamation mark, or bang (!), does not mean anything internally, it is just a naming convention that Ruby programmers use to denote that a method can be “dangerous”.

According to this convention, the exclamation mark(!) must be the last character within the method’s name.

My advice for this is that when you create a bang method it should ideally always have a non bang alternative. In other words, if a bang method exists, its not bang alternative should also exist.

2. They want to get your attention

Bang methods are used to indicate danger, or if your prefer, warning, but, what do they warn us about? Typically it will be one of these two things:

a) Side effect: The object receiving this message will be permanently modified

In Ruby core libraries it is very common that bang methods permanently modify the object getting the message. This does not happen with their non-bang alternative. For example, String#upcase returns the string resulting of uppercasing the original string without modifying it, whereas its non-bang alternative, String#upcase!, permanently uppercases the original string.

b) Exception: This method expects successful execution, otherwise it raises

This use is very common in Ruby on Rails. Take a look at how Rails controllers use update or update! depending on whether you want to check the operation’s result and act in consequence.The non-bang method version returns a boolean value (true if successful, otherwise false), that should be typically checked to decide what to do:

On the other hand its bang counterpart should be used when you assume the operation will always be successful and therefore it should raise when it is not:

3. They are not always “dangerous”

In fact you can and should benefit from them. A clear example of this is to save memory. When a method has both a non-bang and a bang version, the non-bang alternative returns a new instance each time it is executed, whereas the bang version modifies the receiver, which in the case of the non-bang version means higher memory allocation and more work for Ruby’s Garbage Collector when used with collections, which in the long term also increases execution time.

Let’s look at this with an example comparing again upcase with upcase!. We will first create an array of strings and execute GC.start so that the Garbage Collector “cleans” everything before executing our test, then we will measure the memory before and after the execution of each version to know how much extra memory they needed:

upcase

With upcase the result was

upcase!

Whereas with upcase! the result was:

As you can see the non-bang version allocated an extra of 1005MB, whereas the bang alternative did not need any extra memory, so try to remember this every time you have to use these methods with collections 😉

And that is all I wanted to tell you about Ruby’s bang methods! I hope you like it and I hope it was useful for you to learn or refresh concepts. Please remember to suscribe so that you do not miss any updates and if you have any questions please do not hesitate to ask them in the comment section.

See you all next week, adiós!

No Comments

Post A Comment