Question mark at end of method name

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

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

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

Hi –

On Fri, 6 Oct 2006, Paul L. 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.”

Actually the ! at the end of a method name means, according to Matz,
that it’s a “dangerous” method; and such methods always (in the core
at least) exist in a pair with a non-!, non-dangerous method of the
same name.

“Danger” in this context can mean receiver-changing, and often does
mean that, but it doesn’t have to.

David

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

On 10/5/06, py [email protected] 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.”

Le 5 octobre 2006 à 21:47, Richard C. 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

:slight_smile:

Fred
I’ve shown this example to friends once, they think I’m completely
nuts… :>

On 10/5/06, F. Senault [email protected] wrote:

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)

Only true friends can appriciate our qualities.
Robert

Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

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

Regards,
Jordan

Paul L. 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.

Paul L. 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

Paul L. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs