Forum: Ruby The most recommended way of naming methods in Ruby

1279f86519b9a295f6333db870ff3b83?d=identicon&s=25 Edmond Kachale (Guest)
on 2011-01-18 09:17
(Received via mailing list)
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/)
|
Malawi

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

*I wish you a Merry Christmas and a Prosperous New Year 2011!!**
*
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2011-01-18 10:24
(Received via mailing list)
On Jan 18, 2011, at 00:16 , Edmond Kachale 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!
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2011-01-18 10:30
(Received via mailing list)
On Tue, Jan 18, 2011 at 9:16 AM, Edmond Kachale
<edmond.kachale@baobabhealth.org> 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
1279f86519b9a295f6333db870ff3b83?d=identicon&s=25 Edmond Kachale (Guest)
on 2011-01-18 10:41
(Received via mailing list)
2011/1/18 Robert Klemme <shortcutter@googlemail.com>

> On Tue, Jan 18, 2011 at 9:16 AM, Edmond Kachale
> <edmond.kachale@baobabhealth.org> 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/)
|
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.*
*
*
Ac0085dae0703db56ad7f8cb9e1798ba?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2011-01-18 13:30
(Received via mailing list)
On Tue, Jan 18, 2011 at 9:16 AM, Edmond Kachale
<edmond.kachale@baobabhealth.org> 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 Gawlowski

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.
7ff0c83b7ff0249aad31b76ea4f6b3e7?d=identicon&s=25 Shadowfirebird (Guest)
on 2011-01-18 17:22
(Received via mailing list)
> - 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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2011-01-18 17:38
(Received via mailing list)
On Tue, Jan 18, 2011 at 5:21 PM, Shadowfirebird
<shadowfirebird@gmail.com> 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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2011-01-18 21:02
(Received via mailing list)
On Jan 18, 2011, at 7:30 AM, Phillip Gawlowski 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 Wright
1279f86519b9a295f6333db870ff3b83?d=identicon&s=25 Edmond Kachale (Guest)
on 2011-01-19 09:16
(Received via mailing list)
Le 18 janvier 2011 22:01:11 UTC+2, Gary Wright <gwtmp01@mac.com> 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/)
|
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.*
7ff0c83b7ff0249aad31b76ea4f6b3e7?d=identicon&s=25 Shadowfirebird (Guest)
on 2011-01-19 15:45
(Received via mailing list)
> 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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2011-01-19 16:12
(Received via mailing list)
On Wed, Jan 19, 2011 at 3:13 PM, Shadowfirebird
<shadowfirebird@gmail.com> 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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2011-01-19 18:02
(Received via mailing list)
On Jan 19, 2011, at 3:13 AM, Edmond Kachale 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-o...

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

Gary Wright
1279f86519b9a295f6333db870ff3b83?d=identicon&s=25 Edmond Kachale (Guest)
on 2011-01-20 12:40
(Received via mailing list)
2011/1/19 Gary Wright <gwtmp01@mac.com>

> >>
> Here is a great explanation of the '!' naming convention:
>
> <http://dablog.rubypal.com/2007/8/15/bang-methods-o...
>
> 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/)
|
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.*
*
*
D7f817c513d0f32bef489cbc46c71346?d=identicon&s=25 Adam Prescott (Guest)
on 2011-01-20 15:53
(Received via mailing list)
On Tue, Jan 18, 2011 at 12:30 PM, Phillip Gawlowski <
cmdjackryan@googlemail.com> wrote:

>  obj.set_name
>  obj.get_date
>

What is this! Java?

obj.name # get!
obj.date = different_date # set!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.