Forum: Ruby is a "?" at end of method name only for boolean result?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Greg H. (Guest)
on 2008-11-06 00:45
(Received via mailing list)
when should I use ? at the end of a method name?.  for queries that
return (a) boolean value, or (b) any return values? ie any method that
returns data

Thanks
Tim H. (Guest)
on 2008-11-06 01:35
(Received via mailing list)
Greg H. wrote:
> when should I use ? at the end of a method name?.  for queries that
> return (a) boolean value, or (b) any return values? ie any method that
> returns data
>
> Thanks
>

By convention the ? suffix is reserved for methods that return a boolean
  value. Note that all methods return some value, even if it's just nil.
Yukihiro M. (Guest)
on 2008-11-06 02:22
(Received via mailing list)
Hi,

In message "Re: is a "?" at end of method name only for boolean result?"
    on Thu, 6 Nov 2008 07:42:59 +0900, "Greg H."
<removed_email_address@domain.invalid> writes:

|when should I use ? at the end of a method name?.  for queries that
|return (a) boolean value, or (b) any return values? ie any method that
|returns data

By convention, they are predicates, which means the return value can
be considered as boolean value.  But at the same time, the values are
not limited to true and false.  For example, defined? returns the
string to describe the kind of expression defined, and nil otherwise.

There's another convention that when a predicate returns non-true
value for represent true, it should return nil for false.  A bit
confusing phrase, can you get it?

              matz.
Ken B. (Guest)
on 2008-11-06 02:40
(Received via mailing list)
On Wed, 05 Nov 2008 18:32:41 -0500, Tim H. wrote:

> Greg H. wrote:
>> when should I use ? at the end of a method name?.  for queries that
>> return (a) boolean value, or (b) any return values? ie any method that
>> returns data
>>
>> Thanks
>>
>>
> By convention the ? suffix is reserved for methods that return a boolean
>   value. Note that all methods return some value, even if it's just nil.

The defined? operater breaks that convention. It returns nil if the name
it's given is undefined, but a string indicating what kind of object it
is if the name is defined. So you could say the ? suffix is intended for
methods whose outputs are usually treated as boolean.
Michael M. (Guest)
on 2008-11-07 06:41
(Received via mailing list)
Greg H. wrote:
> when should I use ? at the end of a method name?.  for queries that
> return (a) boolean value, or (b) any return values? ie any method that
> returns data
>
> Thanks
>

Is the method asking a question or query?  By the convention, the method
should has a question or query (some_thread.alive? or
some_string.empty?) and it should work in an if statement.  This is only
a convention though, but it could be a little confusing if bend the
rules a little.
This topic is locked and can not be replied to.