tags:

views:

164

answers:

3

I want to know it real meaning of it and how to use it exactly.

Another question is about assert I saw

assert product.valid? product.errors.full_messages and

assert product.valid?

But I can't find syntax for those assert what does second arg for assert (product.errors.full_messages) means or it is arg for ?

Thanks

+1  A: 

It's just convention to put it on a method that returns a boolean afaik. Rather than making everythign IsSomething.

Steven Robbins
+7  A: 

Ruby uses specific naming conventions for methods. It allows you to quickly identify the side effects they may have, or the return type..

These conventions use special markers such as "!" and "?" at the end of method names. This is uncommon since most programming languages tend to forbid such characters in identifiers, but nevertheless, it is truly part of the method name. (and should not be confused with operators)

  • Post fixing "?" means that the method returns a boolean. It's a convenient way to replace the "is" prefix. (this convention tends to exist in lisp dialects too)
  • Post fixing "!" means that the method will modify the instance, and thus won't act on/return a copy.

Note that these are just conventions. In no way you have to follow it, but it is considered a good practice.

Aurélien Vallée
Just to add to this post in this case the '?' and '!' are actually part of the method name and not operators.
rslite
Just to add that ? and ! are just a convention and not a requirement.
Leonid Shevtsov
Truthfully, the purpose of ! mark is to mark a method as special. It doesn’t necessarily mean that it will be destructive or dangerous, but it means that it will require more attention than its alternative. ActiveRecord create! is an example on non destructive use.
khelll
The comments are more accurate than the original response.
Thanatos
+3  A: 

It is purely a convention, it has no formal meaning. It isn't unusual for people to get confused by this, since many (most?) languages don't allow characters such as ? and ! in identifiers.

JesperE