# ! and ? in function name

**URL:** <https://rubytalk.org/t/and-in-function-name/25966>\
**Category:** ruby-talk\
**Created:** [21 March 2006 17:04 UTC](https://rubytalk.org/t/and-in-function-name/25966 "2006-03-21T17:04:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony3](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Tony3](https://rubytalk.org/u/Tony3)\
**Post date:** [21 March 2006 17:04 UTC](https://rubytalk.org/t/and-in-function-name/25966/1 "2006-03-21T17:04:09Z")

</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 😉

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [21 March 2006 17:10 UTC](https://rubytalk.org/t/and-in-function-name/25966/2 "2006-03-21T17:10:45Z")

</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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [21 March 2006 17:11 UTC](https://rubytalk.org/t/and-in-function-name/25966/3 "2006-03-21T17:11:23Z")

</div>

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 Fugal

> **···**
>
> On 3/21/06, Tony \<tony.moutaux@igbmc.u-strasbg.fr\> 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 (!) ?

---

<div class="post-metadata">

**Author:** ![131](https://avatars.discourse-cdn.com/v4/letter/1/50afbb/32.png) [@131](https://rubytalk.org/u/131)\
**Post date:** [21 March 2006 17:12 UTC](https://rubytalk.org/t/and-in-function-name/25966/4 "2006-03-21T17:12:18Z")

</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

> **···**
>
> On 3/21/06, Tony \<tony.moutaux@igbmc.u-strasbg.fr\> 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 😉
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tony3](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Tony3](https://rubytalk.org/u/Tony3)\
**Post date:** [21 March 2006 17:18 UTC](https://rubytalk.org/t/and-in-function-name/25966/5 "2006-03-21T17:18:59Z")

</div>

Thanks to all !

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
