[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

question mark at end of method name

codecraig

10/5/2006 7:12:00 PM

what does the question mark at the end of a method name represent?

for example:

x = "something"
break if x.empty?

i've seen what ! does....what is ?

thanks

10 Answers

Paul Lutus

10/5/2006 7:15:00 PM

0

py wrote:

> what does the question mark at the end of a method name represent?
>
> for example:
>
> x = "something"
> break if x.empty?
>
> i've seen what ! does....what is ?

..empty? means "is it empty?". It tries to suggest that a question is being
asked. It's unfortunate that the "!" syntax, which seems similar, actually
means "apply the result to the present object."

--
Paul Lutus
http://www.ara...

codecraig

10/5/2006 7:18:00 PM

0

Paul Lutus wrote:
> .empty? means "is it empty?". It tries to suggest that a question is being
> asked. It's unfortunate that the "!" syntax, which seems similar, actually
> means "apply the result to the present object."


thanks....couldn't find that in the documentation I've read so far.

Aria Stewart

10/5/2006 7:21:00 PM

0

On Fri, 2006-10-06 at 04:15 +0900, py wrote:
> what does the question mark at the end of a method name represent?
>
> for example:
>
> x = "something"
> break if x.empty?
>
> i've seen what ! does....what is ?

It's just a name. What you do with it is up to you -- Booleanish methods
often read well with a ? at the end, though there's nothing specific
that says it has to be true or false -- could be nil or value, or
whatever else you want to do. It could invoke rm -rf /, even, though I'd
suggest ! for that!

Aria

Aria Stewart

10/5/2006 7:24:00 PM

0

Paul Lutus wrote:

> .empty? means "is it empty?". It tries to suggest that a question is
> being asked. It's unfortunate that the "!" syntax, which seems
> similar, actually means "apply the result to the present object."

Except when it doesn't -- like exit! -- that just means "pay attention,
this isn't your momma's exit. This is the big one, the final, the
do-not-collect-$200 exit.

It's just a name -- but as a lot of people will tell you, names are
important.

Aria

F. Senault

10/5/2006 7:26:00 PM

0

Le 5 octobre 2006 à 21:11, py a écrit :

> i've seen what ! does....what is ?

Those are conventions. ? and ! have no real meaning in that context for
the ruby parser in itself.

The first just usually suggests the method called is a query type which
returns a boolean type, while the second usually means the method will
have some destructive or important effect on the object being
manipulated ; for instance, s.downcase will return a copy of s in lower
case, while s.downcase! will actually modify s in place.

Fred
--
Trapped in purgatory A lifeless object, alive Awaiting reprisal Death
will be their acquisition The sky is turning red Return to power draws
near Fall into me, the sky's crimson tears Abolish the rules made of
stone (Raining Blood, Slayer)

dblack

10/5/2006 7:34:00 PM

0

Richard Conroy

10/5/2006 7:48:00 PM

0

On 10/5/06, py <codecraig@gmail.com> wrote:
> what does the question mark at the end of a method name represent?

Unlike other languages which generally only permit alphanumerics in
method names, Ruby allows the use of a limited number of punctuation
characters to pad out function names.

This isn't a special syntax. There are plenty others too.

By convention, the trailing '?' in a method definition signifies that this
function call will return a boolean value, and its a wonderfully expressive
and pleasant way to do this. Little things like this make people love
languages.

The '!' is a bit different - its generally used to indicate that calling the
function can change the objects state or modify it (if you weren't
really expecting it). The convention isn't as tight, and the APIs
usually offer you a safe version of the same function too.

I guess that omitting a '?' from a function that returns exclusively
boolean values would be considered bad Ruby style.

I am on shaky ground here, personally, but I believe Ruby's way
of doing operators uses the same thing. If you look at the '<<'
operator (which does 'append to me') in say the string or array
class, you will find a method defintion like this:

def <<(variable)
#implementation of append e.g.
concat(variable)
end

So you should be doing wierd stuff like:

String.<<(anotherString)

but Ruby steps in with some syntactic sugar
and you can drop all the punctuation:

String << anotherString

The rules are probably similar for the other operators,
like FixNum probably has a definition like this

def + anotherFixNum
....
end

(this is the bit where my mouth runs off and leaves my
knowledge behind)

Basically when you are used to other languages that stuff
looks really wierd - it doesn't even look like a proper
definition, but once you understand it, its really elegant:

"Oh the plus sign is the *name* of the function? Neat."

F. Senault

10/5/2006 7:58:00 PM

0

Le 5 octobre 2006 à 21:47, Richard Conroy a écrit :

> The rules are probably similar for the other operators,
> like FixNum probably has a definition like this
>
> def + anotherFixNum
> ....
> end
>
> (this is the bit where my mouth runs off and leaves my
> knowledge behind)

Well, in the "don't try this at home kids" area, I've always been
fascinated by this :

>> class Fixnum
>> def +(a)
>> self * a
>> end
>> end
=> nil
>> 5 + 5
=> 25

:)

Fred
I've shown this example to friends once, they think I'm completely
nuts... :>
--
How can I know which way to turn
Between my feelings and my dreams
I'm drowning in tennement oceans
There is more than this (Texas, In my Heart)

MonkeeSage

10/5/2006 9:59:00 PM

0

Ps. Such methods (returning exclusively bool) are often called
"predicates".

Regards,
Jordan

Martin Coxall

10/6/2006 8:46:00 AM

0

> Paul Lutus wrote:
> > .empty? means "is it empty?". It tries to suggest that a question is being
> > asked. It's unfortunate that the "!" syntax, which seems similar, actually
> > means "apply the result to the present object."
>
>
> thanks....couldn't find that in the documentation I've read so far.

That's almost certainly because it's a convention rather than a
feature of the language. Generally, conventions of languages tend to
be less well documented, because they change over time.

Martin