David Black Explains Bang Methods

Eric Hodel | Wed, 15 Aug 2007 18:23:07 GMT

Posted in

I’ve seen an explosion of silly bang methods that don’t follow Ruby’s usage, and I’ve been meaning to write exactly this post:

In Ruby, you can write methods whose names end in ! (exclamation point or “bang”). There’s a lot of confusion surrounding the matter of when, and why, you would want to do so.

The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name.

Bang methods; or, Danger, Will Rubyist! via DABlog 7 comments

Comments RSS FEED

I sort of disagree with the last statement that ”! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name.” For me it’s like, before you’re calling something that ends with !, know what you’re doing. And make sure you’re doing it right. What David said, is based on the assumption that your method names should reflect the “danger”. Well, that may not be always possible and it’s a different issue altogether.

Pratik said about 1 hour later

Hi Pratik—

The thing is, you should always know what you’re doing :-) Putting a ! on a subset of the methods you write, whether or not they are part of a “dangerous/non-dangerous” pair, just arbitrarily chooses some methods to provide a contextless warning for, and means that there’s no real ! idiom or convention.

David Black said about 2 hours later

The lack of a non-bang version of a method just makes me scratch my head. In Ruby’s core library the bang provides context to tell me that this method does something different, probably not what I want. Indiscriminate use of the bang, especially when not paired with non-bang versions, provides me with no extra information.

Eric Hodel said about 4 hours later

I was under the impression that in most cases the bang meant this method is going to modify this (self) object, and not just return a copy of the modified object. An example of this would be the String instance method capitalize.


str = "hello" 
str.capitalize #=> "Hello" 
str #=> "hello" 

str.capitalize! #=> "Hello" 
str #=> "Hello" 
James Hill said about 5 hours later

In the ruby core, that’s usually the case.

People have heard this self-modifying behavior described as “dangerous” without finding out the full meaning which typically is “dangerous with respect to the non-bang version”.

Eric Hodel said about 6 hours later

James—

Your impression is empirically correct - lots of destructive methods are !-methods - but that’s not the fundamental meaning of the !. “Dangerous” can mean many things, and destructive methods may or may not have a !. The only ones that do are those that exist in a pair with a non-destructive counterpart.

There’s also exit! which is an example of a non-destructive bang-method. It’s the “dangerous” version of exit.

The ! convention is also something you can use in your own code. That’s another good reason not to conflate it with “destructive”; that puts a limit on its use.

David Black said about 7 hours later

This might be the Ruby convention, and I’m willing to follow it if so. I’ve programmed in various dialects of LISP where ! means the procedure has a side-effect. I find that convention absolutely invaluable, and this vague notion of “danger” where ! disambiguates the “safe” from the “dangerous” method seems odd. I’d rather the convention be more like in Scheme: Side-effect? bang!

Nick Kallen said 7 days later

Comments are disabled