Bang Methods In Ruby
After working with so many other languages, one of the unusual things that stood out with the Ruby language was the way that the '!' symbol was used with methods.
Why were there methods like
reverse!? How does this impact the code? This short post looks to address just that.
When we are talking about collections, something that you may find unintuitive is that certain methods such as
reverse do not actually modify the collection.
What do I mean? Well open up a REPL such as
irb and you can see what I am talking about in action:
irb(main):001:0> a = [1,2,3] => [1, 2, 3] irb(main):002:0> a.reverse => [3, 2, 1] irb(main):003:0> a => [1, 2, 3]
Notice that although
a.reverse returns a new collection,
a is not modified. We can further demonstrate this by assigning the value of
a.reverse to another variable.
irb(main):004:0> b = a.reverse => [3, 2, 1] irb(main):005:0> b => [3, 2, 1] irb(main):006:0> a => [1, 2, 3]
Above, you can see that
b is in fact the reverse of
a itself was not modified.
What happens if we want to modify the collection in place? Well, we could reassign
a = a.reverse, but even better is to use the bang method
reverse! to accomplish this.
irb(main):011:0> a => [1, 2, 3] irb(main):012:0> a.reverse! => [3, 2, 1]
So does this mean that the bang
! symbol on methods will only be used for altering collections? Not quite.
In Ruby, the convention is that the bang
! symbol is used for to indicate that the method is the "dangerous" or surprising version of a pair of methods. That means that there are methods without the
! that will still modify the data. Some more obvious examples of this (when talking about collections) are
Bang in the wild
Another common example where the
! alternative method shows up is when dealing with Ruby on Rails.
save methods come with the alternate pair
So what's the difference?
The documentation has more, but the most prominent different is that the
! method will raise an exception if the method fails (
save! respectively), while the bang-less alternative will return
false if the method fails.
Whenever you see the
! in the name of a method, the best thing you can do is refer to the relevant documentation to define what the difference between both implementations of the methods are.
Today's post is a short overview on pattern matching, how it differs from case statements and how it can be used to solve a variety of matching problems.
Resources and further reading
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.