# The most recommended way of naming methods in Ruby

**URL:** <https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930>\
**Category:** Ruby\
**Created:** [January 18, 2011, 9:17am UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930 "2011-01-18T09:17:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 18, 2011, 9:17am UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/1 "2011-01-18T09:17:47Z")

</div>

Rubists,

What is the most recommended (or the conventional) way of naming methods  
in  
Ruby? Do they need to be verb-like or noun-like? In other language like  
C++,  
methods are supposed to be verbs because they are dimmed as messages  
sent to  
the object. I have asked this because methods in Ruby are also objects.

* * *

Edmond (ceekays)  
Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/](http://www.baobabhealth.org/))  
|  
Malawi

Cell: +265 999 465 137 | +265 881 234 717

_I wish you a Merry Christmas and a Prosperous New Year 2011!!_\*  
\*

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 10:24am UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/2 "2011-01-18T10:24:54Z")

</div>

On Jan 18, 2011, at 00:16 , Edmond K. wrote:

> What is the most recommended (or the conventional) way of naming methods in  
> Ruby? Do they need to be verb-like or noun-like? In other language like C++,  
> methods are supposed to be verbs because they are dimmed as messages sent to  
> the object. I have asked this because methods in Ruby are also objects.

verbs\_underscore\_separated  
predicate?  
dangerous\_mutator!

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 10:30am UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/3 "2011-01-18T10:30:33Z")

</div>

On Tue, Jan 18, 2011 at 9:16 AM, Edmond K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the most recommended (or the conventional) way of naming methods in  
> Ruby? Do they need to be verb-like or noun-like? In other language like C++,  
> methods are supposed to be verbs because they are dimmed as messages sent to  
> the object. I have asked this because methods in Ruby are also objects.

Not exactly. There was a lengthy discussion here about whether  
methods are objects or not. Strictly speaking they are not, but you  
can obtain an object representing a bound or unbound method.

> _I wish you a Merry Christmas and a Prosperous New Year 2011!!_\*

ed.update\_signature!

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 10:41am UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/4 "2011-01-18T10:41:52Z")

</div>

2011/1/18 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> On Tue, Jan 18, 2011 at 9:16 AM, Edmond K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > _I wish you a Merry Christmas and a Prosperous New Year 2011!!_\*
> 
> ed.update\_signature!

Lol!

\*\>\> ed.updated?

> > true  
> > ed.signature\*\*.datetime  
> > Mon Jan 03 08:00:25 0200 2011

- 

That’s what happens when one is using offline mail clients. My offline  
mail  
client for some reason is getting an old signature.

* * *

Edmond(ceekays)  
Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/](http://www.baobabhealth.org/))  
|  
Malawi

Cell: +265 999 465 137 | +265 881 234 717\*  
\*\*  
An old dog does not hunt because of speed, but his endurance of the  
heart.\*  
\*  
\*

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 5:22pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/5 "2011-01-18T17:22:18Z")

</div>

> - Interrogative methods get phrased as questions:  
> obj.date\_today?  
> obj.name?  
> obj.calculation\_done?

As you said; but I would only use this interrogative form myself if the  
method returned true/false. That’s how it works in core Ruby?

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 5:38pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/6 "2011-01-18T17:38:38Z")

</div>

On Tue, Jan 18, 2011 at 5:21 PM, Shadowfirebird  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > - Interrogative methods get phrased as questions:  
> > obj.date\_today?  
> > obj.name?  
> > obj.calculation\_done?
> 
> As you said; but I would only use this interrogative form myself if the method  
> returned true/false. That’s how it works in core Ruby?

It’s more relaxed since you can use any object reference as boolean  
value. For example, you may do

class X  
attr\_accessor :name  
alias name? name  
end

if the test should return a trueish value if the name is set (actually  
not nil and not false).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 9:02pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/7 "2011-01-18T21:02:13Z")

</div>

On Jan 18, 2011, at 7:30 AM, Phillip G. wrote:

> - methods that modify the object (or caller) itself, should be exclamations:  
> obj.truncate!  
> obj.remove\_name!  
> obj.date! “Julian” # I wish I could do obj.date “Julian”!, instead. Ah, well.

I think the conventional wisdom is that this should only be done only if  
there is a matching non-mutating method that has the name without the  
exclamation mark.

Gary W.

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 18, 2011, 1:30pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/8 "2011-01-18T13:30:59Z")

</div>

On Tue, Jan 18, 2011 at 9:16 AM, Edmond K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rubists,
> 
> What is the most recommended (or the conventional) way of naming methods in  
> Ruby? Do they need to be verb-like or noun-like? In other language like C++,  
> methods are supposed to be verbs because they are dimmed as messages sent to  
> the object. I have asked this because methods in Ruby are also objects.

Well, I can’t speak for Rubyists in general, but I aim for the  
following:

- methods that do something should be verbs:  
obj.calculate  
obj.set\_name  
obj.get\_date

- methods that are accessors (or behave like them) should be nouns:  
foo = obj.name  
puts obj.date  
obj.calculation

- Interrogative methods get phrased as questions:  
obj.date\_today?  
obj.name?  
obj.calculation\_done?

- methods that modify the object (or caller) itself, should be  
exclamations:  
obj.truncate!  
obj.remove\_name!  
obj.date! “Julian” # I wish I could do obj.date “Julian”!, instead.  
Ah, well.

–  
Phillip G.

Though the folk I have met,  
(Ah, how soon!) they forget  
When I’ve moved on to some other place,  
There may be one or two,  
When I’ve played and passed through,  
Who’ll remember my song or my face.

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 19, 2011, 3:45pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/9 "2011-01-19T15:45:36Z")

</div>

> It’s more relaxed since you can use any object reference as boolean  
> value. For example, you may do
> 
> class X  
> attr\_accessor :name  
> alias name? name  
> end
> 
> if the test should return a trueish value if the name is set (actually  
> not nil and not false).

What, “classname = Myclass.name?”? Ew. Sorry, I wouldn’t be  
comfortable with that. Where in the core Ruby classes does that happen?

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 19, 2011, 9:16am UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/10 "2011-01-19T09:16:02Z")

</div>

Le 18 janvier 2011 22:01:11 UTC+2, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a crit  
:

> I think the conventional wisdom is that this should only be done only if  
> there is a matching non-mutating method that has the name without the  
> exclamation mark.

Someone may give more info on why it should be like this:

* * *

Edmond  
Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/](http://www.baobabhealth.org/))  
|  
Malawi

Cell: +265 999 465 137 | +265 881 234 717\*  
\*\*  
An old dog does not hunt because of speed, but his endurance of the  
heart.\*

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 19, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/11 "2011-01-19T16:12:00Z")

</div>

On Wed, Jan 19, 2011 at 3:13 PM, Shadowfirebird  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What, “classname = Myclass.name?”? Ew. Sorry, I wouldn’t be comfortable with  
> that. Where in the core Ruby classes does that happen?

I am not sure what you are hinting at. Clarification - just in case:  
I did _not_ redefine the class’s name in the example above.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 19, 2011, 6:02pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/12 "2011-01-19T18:02:03Z")

</div>

On Jan 19, 2011, at 3:13 AM, Edmond K. wrote:

> > well.
> > 
> > I think the conventional wisdom is that this should only be done only if  
> > there is a matching non-mutating method that has the name without the  
> > exclamation mark.
> 
> Someone may give more info on why it should be like this:

I think you left something off. Anyway…

Here is a great explanation of the ‘!’ naming convention:

[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)

I was going to try to explain this but why bother when David Black has  
already done a great job?

Gary W.

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 20, 2011, 3:53pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/13 "2011-01-20T15:53:45Z")

</div>

On Tue, Jan 18, 2011 at 12:30 PM, Phillip G. \<  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> obj.set\_name  
> obj.get\_date

What is this! Java?

obj.name # get!  
obj.date = different\_date # set!

---

<div class="post-metadata">

**Author:** ![ceekays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5f9b8f/32.png) [@ceekays](https://www.ruby-forum.com/u/ceekays)\
**Post date:** [January 20, 2011, 12:40pm UTC](https://www.ruby-forum.com/t/the-most-recommended-way-of-naming-methods-in-ruby/201930/14 "2011-01-20T12:40:09Z")

</div>

2011/1/19 Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > >
> 
> Here is a great explanation of the ‘!’ naming convention:
> 
> [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)
> 
> I was going to try to explain this but why bother when David Black has  
> already done a great job?

I like you on this.

* * *

Edmond  
Software Developer | Baobab Health Trust ([http://www.baobabhealth.org/](http://www.baobabhealth.org/))  
|  
Malawi

Cell: +265 999 465 137 | +265 881 234 717\*  
\*\*  
An old dog does not hunt because of speed, but his endurance of the  
heart.\*  
\*  
\*
