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

G

Greg Hauptmann

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
 
T

Tim Hunter

Greg said:
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.
 
K

Ken Bloom

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.
 
M

Michael Morin

Greg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top