# Method! and method?

**URL:** <https://www.ruby-forum.com/t/method-and-method/194970>\
**Category:** Ruby\
**Created:** [September 14, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/method-and-method/194970 "2010-09-14T16:12:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 14, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/method-and-method/194970/1 "2010-09-14T16:12:37Z")

</div>

I am trying to find the meaning of ! and ? appended to method names but  
I cannot find what they mean. Can someone explain?

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 14, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/method-and-method/194970/2 "2010-09-14T16:44:15Z")

</div>

Gene A. wrote:

> I am trying to find the meaning of ! and ? appended to method names but  
> I cannot find what they mean. Can someone explain?

They don’t mean anything - they are just part of the name. That is, in

str.downcase!

the method you are calling is called :downcase! - which is a distinct  
method to :downcase

However, there are conventions as to when they should be used. Methods  
ending ? typically query the properties of some object and return a  
boolean-like result, while methods ending ! are “dangerous” in some way,  
such as modifying the state of the object when a non-modifying  
alternative method is available.

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 14, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/method-and-method/194970/3 "2010-09-14T16:47:23Z")

</div>

On Tue, Sep 14, 2010 at 4:12 PM, Gene A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to find the meaning of ! and ? appended to method names but  
> I cannot find what they mean. Can someone explain?

From the language functionality point of view, they don’t mean  
anything. They just happen to be valid characters for method names,  
and people can use them to give more readability to their code.

The core and stdlib classes, though, typically use ! to denote a  
“dangerous” method. For example a method that modifies the receiver or  
some side effect. They usually come in pairs with their counterpart  
having the same name without the !, and usually it means that they  
both do the same task, but the ! version will modify the receiver. See  
for example String#gsub and String#gsub!. The ? on the other hand is  
typically used when the method is a query method, when calling the  
method would be like asking the object something. Things like  
Enumerable#include? or Object#equal?

Please take note that these are just conventions of method names. You  
will find destructive or dangerous methods without a !, and query  
methods without a ?, and possibly the other way around.

Jesus.

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 14, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/method-and-method/194970/4 "2010-09-14T16:47:58Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Tuesday 14 September 2010, Gene A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to find the meaning of ! and ? appended to method names but  
> I cannot find what they mean. Can someone explain?

First of all, it’s a convention, nothing hard-wired.

The question mark is probably the easier to explain: It marks a boolean-  
returning getter method. Where in e.g. Java you’d write isFile(…) or  
getStuff(…), with Ruby, it’s just file? or stuff?.

The exclamation mark is harder to grasp. It’s meaning is along the lines  
of  
“pay attention with this method.” Most commonly it is used to demark a  
destructive twin of another method. E.g., there can be a #sort and a  
#sort!  
method. While the first one would return a sorted copy of the original,  
the  
second would modify the original itself. There is, however, no sense in  
marking every destructive method with an exclamation mark. A File#delete  
method is obvious enough, there’s no need for a File#delete!. Think of  
it as  
the evil twin of the one without the exclamation mark. 🙂

HTH.

```
  Eric

```

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkyPiJsACgkQhS0drJ3goJIGbQCcDAio4v10CDh5IeFYWW8/Dd2t  
NgIAnRjtcep7/AY/s3g3xeYvsRuLkloK  
=IJsL  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 14, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/method-and-method/194970/5 "2010-09-14T17:11:24Z")

</div>

Gene A. wrote:

> I am trying to find the meaning of ! and ? appended to method names but  
> I cannot find what they mean. Can someone explain?

They aren’t appended to the method name. They are part of the method  
name. A method ending in ‘!’ or ‘?’ means pretty much the same as a  
method name ending in ‘a’ or ‘z’: it means whatever the author of that  
method wants it to mean.

There is a convention, however: a method name ending in ‘?’ usually  
indicates a method that returns either a truthy or a falsy value. For  
example, take the `Integer#even?` method. An integer is either even or  
it’s not. Or `Array#empty?`, `Numeric#zero?`, `Time#thursday?`: an  
array is either empty or not, a number is either zero or not, a  
specific date is either a thursday or not.

For a method name ending in ‘!’, the convention is the following: if  
there are two methods that both perform the same operation, but in a  
slightly different way, then they both get the same name, but the one  
which is more “surprising” gets an exclamation point appended. For  
eample: `String#downcase` downcases as string. It returns a new  
downcased string, but leaves the original untouched.  
`String#downcase!` _also_ downcases a string, but _it_ destroys the  
original string and _replaces_ it with a downcased version.

jwm

---

<div class="post-metadata">

**Author:** ![mydarkpassenger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@mydarkpassenger](https://www.ruby-forum.com/u/mydarkpassenger)\
**Post date:** [September 14, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/method-and-method/194970/6 "2010-09-14T17:00:51Z")

</div>

This blog  
post[http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist](http://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyist)has  
an interesting perspective on the topic.
