Newbie Question about Array#include?

A

AoP

Why does the "include?" method of Array end with a question mark? I
thought the question mark was to indicate some sort of "unexpected"
behavior, such as modifying the list, but it doesn't seem to in this
case.

Thanks -

-James
 
S

Stefano Crocco

Alle Sunday 20 January 2008, AoP ha scritto:
Why does the "include?" method of Array end with a question mark? I
thought the question mark was to indicate some sort of "unexpected"
behavior, such as modifying the list, but it doesn't seem to in this
case.

Thanks -

-James

You're thinking of the exclamation mark (!), which is used for 'dangerous'
methods or to distinguish methods which modifies the receiver from the
analogous method wich doesn't (for example, Array#uniq! and Array#uniq). The
question mark, instead, is used for methods which express a predicate or, in
other words, which answer a question (for example, Array#include? answers the
question 'does the array contain this item?')

Stefano
 
T

Tim Hunter

AoP said:
Why does the "include?" method of Array end with a question mark? I
thought the question mark was to indicate some sort of "unexpected"
behavior, such as modifying the list, but it doesn't seem to in this
case.

Thanks -

-James

You're thinking of !, not ?. There are no hard-and-fast rules, but
typically methods that end in ? return true or false, and methods that
end in ! need extra attention for one reason or another.
 
A

AoP

question mark, instead, is used for methods which express a predicate or, in
other words, which answer a question (for example, Array#include? answers the
question 'does the array contain this item?')


Thank you!
 
J

John Joyce

You're thinking of !, not ?. There are no hard-and-fast rules, but
typically methods that end in ? return true or false, and methods
that end in ! need extra attention for one reason or another.
Also remember that there are some methods in some classes that do not
have ! but do modify the receiver.
Array has .push and .pop for examples.

In many languages, equivalents of Ruby methods ending with ? are
called predicates. They usually read like an English Yes/No or True/
False question.
 

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,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top