# ! and ? in function name

**URL:** <https://www.ruby-forum.com/t/and-in-function-name/55499>\
**Category:** Ruby\
**Created:** [March 21, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/and-in-function-name/55499 "2006-03-21T18:03:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [March 21, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/and-in-function-name/55499/1 "2006-03-21T18:03:50Z")

</div>

Hi Ruby-rulez !

I often look at some code, for example un rails/active\_record, where  
functions name end with a ? or a !

Is this a special notation, meaning that the func or args or whatever  
have some treatment before/after the body ? Or to show that the function  
return a boolean (?) and the arg modified (!) ?

Thanx 😉

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [March 21, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/and-in-function-name/55499/2 "2006-03-21T18:10:28Z")

</div>

Tony wrote:

> Hi Ruby-rulez !
> 
> I often look at some code, for example un rails/active\_record, where  
> functions name end with a ? or a !
> 
> Is this a special notation, meaning that the func or args or whatever  
> have some treatment before/after the body ? Or to show that the function  
> return a boolean (?) and the arg modified (!) ?
> 
> Thanx 😉

Generally the (?) means it returns a boolean and (!) means it modifies  
the receiver (the object the method is being called on) in place, rather  
than creating a new object.

For example:

irb(main):001:0\> a = “Hello”  
=\> “Hello”  
irb(main):002:0\> a.include?(‘e’)  
=\> true  
irb(main):003:0\> a.reverse  
=\> “olleH”  
irb(main):004:0\> a  
=\> “Hello” #unmodified  
irb(main):005:0\> a.reverse!  
=\> “olleH” #modified  
irb(main):006:0\> a  
=\> “olleH”

Hope that helps.

-Justin

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [March 21, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/and-in-function-name/55499/3 "2006-03-21T18:11:28Z")

</div>

On 3/21/06, Tony [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Ruby-rulez !
> 
> I often look at some code, for example un rails/active\_record, where  
> functions name end with a ? or a !
> 
> Is this a special notation, meaning that the func or args or whatever  
> have some treatment before/after the body ? Or to show that the function  
> return a boolean (?) and the arg modified (!) ?

The latter. The ? and ! characters in a method name have absolutely no  
import to the parser/interpreter (well, no more import than does any  
other character such as ‘f’ or ‘q’). They only serve, by convention,  
as an indicator to the programmer of the behavior of the method.

Jacob F.

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [March 21, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/and-in-function-name/55499/4 "2006-03-21T18:12:28Z")

</div>

Hi,

Here is an excerpt from the free online version of the Programming  
Ruby book [1]:

…  
Methods that act as queries are often named with a trailing `?'', such as instance_of?. Methods that are `dangerous,‘’ or modify the  
receiver, might be named with a trailing `!''. For instance, String provides both a chop and a chop!. The first one returns a modified string; the second modifies the receiver in place. `?‘’ and ``!‘’ are  
the only weird characters allowed as method name suffixes.  
…

[1] [http://www.rubycentral.com/book/tut\_methods.html](http://www.rubycentral.com/book/tut_methods.html)

Martins

---

<div class="post-metadata">

**Author:** ![tony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@tony](https://www.ruby-forum.com/u/tony)\
**Post date:** [March 21, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/and-in-function-name/55499/5 "2006-03-21T18:18:42Z")

</div>

Thanks to all !

And specially Martin, because I read the given page twice today but  
never the first paragraph, answering my (stupid) question.
