The One Thing You Need To Know About Bang Methods
You have probably stumbled across code like this (or maybe written it yourself):
You likely took this as an indication of a method invocation with serious consequences. But what about this:
Hmmm. One would think that demolition of a house would qualify as a pretty serious. Why then, doesn’t
#destroy end with a bang?
Let’s look at a few examples:
No exclamations to be seen. Now let’s look at instances where a bang is actually used:
Or the example of a well known library:
If you look at these carefully, you’ll notice 2 things:
There is a version of the method that doesn’t end with a bang, and one that does.
The method version ending with a bang doesn’t necessarily imply a more destructive behavior.
So when should you actually end a method with a bang?
You should do it only when you want to convey a slight variation in behavior versus the non-bang version—one that doesn’t justify giving the alternative version a completely different name.
There, now you know the secret. Now go forth and write more idiomatic Ruby. And stop writing all those annoying bangs!