0

Possible Duplicate:
Why are exclamation marks used in Ruby methods?

While looking over some Ruby code I came across this method:

def SomeMethod!
    // ...
end

What does the ! following the method name do?

Community
  • 1
  • 1
SundayMonday
  • 19,147
  • 29
  • 100
  • 154

3 Answers3

2

I believe it's just a convention to mark a method as 'more dangerous than others." This could mean that it has a side effect that affects its parameters or maybe its class attributes. It's just a reminder to be careful.

Mirthquakes
  • 343
  • 1
  • 10
1

By convention it means that the method mutates the state of the class.

Frank
  • 3,029
  • 5
  • 34
  • 43
1

The exclamation mark at the end of a method's name means that such a method performs an operation in a more dangerous way than the version of the same method without the !.

Notice that you should never ever use ! if there is not such other method. You can understand ! as a means to visually differentiate between two otherwise equivalent methods.

Example:

Array#flatten

Array#flatten!

Kernel#exit!

Kernel#exit

Nerian
  • 15,901
  • 13
  • 66
  • 96
  • +1 for correctly mentioning the fact that bang methods should only exist if there is a corresponding non-bang method. -1 for mentioning mutation: it's not *just* about mutation, it's basically about being "more surprising", which includes, but is not limited to mutation. – Jörg W Mittag Oct 22 '11 at 01:18
  • @JörgWMittag: 'surprising' is kind of vague :) Can you give me an example on the MRI where `!` are used to signal something else than mutation of the original data? – Nerian Oct 22 '11 at 10:24
  • [`Kernel#exit!`](http://RubyDoc.Info/stdlib/core/Kernel:exit!) and [`Process::exit!`](http://RubyDoc.Info/stdlib/core/Process.exit!). – Jörg W Mittag Oct 22 '11 at 11:09