Is a "?" at end of method name only for boolean result?


#1

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


#2

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.


#3

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.

#4

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.


#5

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.