Method! and method?

I am trying to find the meaning of ! and ? appended to method names but
I cannot find what they mean. Can someone explain?

Gene A. wrote:

I am trying to find the meaning of ! and ? appended to method names but
I cannot find what they mean. Can someone explain?

They don’t mean anything - they are just part of the name. That is, in

str.downcase!

the method you are calling is called :downcase! - which is a distinct
method to :downcase

However, there are conventions as to when they should be used. Methods
ending ? typically query the properties of some object and return a
boolean-like result, while methods ending ! are “dangerous” in some way,
such as modifying the state of the object when a non-modifying
alternative method is available.

On Tue, Sep 14, 2010 at 4:12 PM, Gene A. [email protected] wrote:

I am trying to find the meaning of ! and ? appended to method names but
I cannot find what they mean. Can someone explain?

From the language functionality point of view, they don’t mean
anything. They just happen to be valid characters for method names,
and people can use them to give more readability to their code.

The core and stdlib classes, though, typically use ! to denote a
“dangerous” method. For example a method that modifies the receiver or
some side effect. They usually come in pairs with their counterpart
having the same name without the !, and usually it means that they
both do the same task, but the ! version will modify the receiver. See
for example String#gsub and String#gsub!. The ? on the other hand is
typically used when the method is a query method, when calling the
method would be like asking the object something. Things like
Enumerable#include? or Object#equal?

Please take note that these are just conventions of method names. You
will find destructive or dangerous methods without a !, and query
methods without a ?, and possibly the other way around.

Jesus.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 14 September 2010, Gene A. [email protected] wrote:

I am trying to find the meaning of ! and ? appended to method names but
I cannot find what they mean. Can someone explain?

First of all, it’s a convention, nothing hard-wired.

The question mark is probably the easier to explain: It marks a boolean-
returning getter method. Where in e.g. Java you’d write isFile(…) or
getStuff(…), with Ruby, it’s just file? or stuff?.

The exclamation mark is harder to grasp. It’s meaning is along the lines
of
“pay attention with this method.” Most commonly it is used to demark a
destructive twin of another method. E.g., there can be a #sort and a
#sort!
method. While the first one would return a sorted copy of the original,
the
second would modify the original itself. There is, however, no sense in
marking every destructive method with an exclamation mark. A File#delete
method is obvious enough, there’s no need for a File#delete!. Think of
it as
the evil twin of the one without the exclamation mark. :slight_smile:

HTH.

  Eric

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkyPiJsACgkQhS0drJ3goJIGbQCcDAio4v10CDh5IeFYWW8/Dd2t
NgIAnRjtcep7/AY/s3g3xeYvsRuLkloK
=IJsL
-----END PGP SIGNATURE-----

Gene A. wrote:

I am trying to find the meaning of ! and ? appended to method names but
I cannot find what they mean. Can someone explain?

They aren’t appended to the method name. They are part of the method
name. A method ending in ‘!’ or ‘?’ means pretty much the same as a
method name ending in ‘a’ or ‘z’: it means whatever the author of that
method wants it to mean.

There is a convention, however: a method name ending in ‘?’ usually
indicates a method that returns either a truthy or a falsy value. For
example, take the Integer#even? method. An integer is either even or
it’s not. Or Array#empty?, Numeric#zero?, Time#thursday?: an
array is either empty or not, a number is either zero or not, a
specific date is either a thursday or not.

For a method name ending in ‘!’, the convention is the following: if
there are two methods that both perform the same operation, but in a
slightly different way, then they both get the same name, but the one
which is more “surprising” gets an exclamation point appended. For
eample: String#downcase downcases as string. It returns a new
downcased string, but leaves the original untouched.
String#downcase! also downcases a string, but it destroys the
original string and replaces it with a downcased version.

jwm

This blog
posthttp://dablog.rubypal.com/2007/8/15/bang-methods-or-danger-will-rubyisthas
an interesting perspective on the topic.