-1

Possible Duplicate:
Why are exclamation marks used in Ruby methods?

Hi All,

In ruby/rails I have seen some methods ended up with ! sign like

before_filter :authenticated!

just wonder what does mean by authenticated!, '!' sign

thanks in advance

cheers

sameera

Community
  • 1
  • 1
sameera207
  • 16,547
  • 19
  • 87
  • 152

9 Answers9

2

In Ruby, certain types of punctuation are used at the ends of method names to denote some type of special functionality. The exclamation mark is used to mark functions that change one or more of the parameters passed into them. See the difference between gsub and gsub! for instance.

The other common one is the question mark, which denotes that the method is a question about the object and returns a boolean. For instance, include?. Most of these functions if implemented in other languages will typically start with an is or has prefix or similar.

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
2

As a convention, identifier? are used as predicate names, and identifier! are used for the more destructive (or more dangerous) methods than the method which have same name without !.

Source

Example:

lstrip will return a new string with leading spaces removed thus original string is un-modified. lstrip! will modify the string-in place

Zabba
  • 64,285
  • 47
  • 179
  • 207
2

the difference: by example (gsub/gsub!)

string = "this_string"
string.gsub!("this", "that") 
puts string #=> string = "that_string"

string = "this_string"
string.gsub("this, "that")
puts string #=> string is stil = "this_string"

gsub without ! will return the value, but will not modify the original string. this is generally how the ! is used, without it nothing is modified. with it the original is modified

Paul Kaplan
  • 2,885
  • 21
  • 25
1

You can name every method you want with an exclamation mark at the end. However, the convention is that you use that syntax when methods are in any way intrusive. So, for example on an object of class Car, the method fast? should return true or false but fast! should perhaps change some instance variable @fast to true.

moritz
  • 25,477
  • 3
  • 41
  • 36
1

! can be used as last character in Ruby method names (as can ? and =). It's often used to denote "dangerous" methods (e.g. mutating a value in place) or generally methods you should pay attention to.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
1

if you ask most rails guys, they will say the bang version (!) means the method will throw an exception.

What I prefer is Greg Browns definition in Ruby Best Practices, which is that a bang is to indicate that there are two versions of a very similar method, but the bang one you need to pay more attention to

Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
1

Methods ending in ! usually transforms the object it is called upon as opposed to the methods without ! which performs an action and returns a new object.They are termed as "dangerous methods" in Ruby as they may change state that someone else might have a reference to.
Examples of such pairs of methods include sort/sort! for arrays, upcase/upcase! for strings, chomp/chomp! for strings, and reverse/reverse! for strings and arrays. In each case, if you call the non-bang version of the method on the object, you get a new object. If you call the bang version, you operate in-place on the same object to which you sent the message.

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
1

The ! character is used to denote a destructive method: that is a method that permanently alters one or more of the variables passed to it.

For example:

Pretend you have a plus_one method that adds 1 to the number passed to it.

if we have

a = 1

Then we could do

b=a.plus_one

and still have a = 1. However if plus_one were a destructive method (and as a result were called with plus_one!) doing the following:

     a.plus_one!

would permanently alter the value of a to be 2.

  • Too bad integers are immutable objects so this example irks me; you can't have a destructive method on integers. – kentor Mar 16 '12 at 05:32
1

Exclamation marks are used by convention in instance methods just to denote that the method is going to modify the instance itself.

While

mystring.gsub(..)

won't modify mystring,

mystring.gsub!(..)

will.

Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67