Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are exclamation marks used in Ruby methods?

In Ruby some methods have a question mark (?) that ask a question like include? that ask if the object in question is included, this then returns a true/false.

But why do some methods have exclamation marks (!) where others don't?

What does it mean?

like image 929
Lennie Avatar asked Mar 04 '09 20:03

Lennie


People also ask

What does double exclamation mark mean in Ruby?

In most programming languages, including Ruby, ! will return the opposite of the boolean value of the operand. So when you chain two exclamation marks together, it converts the value to a boolean.

What does def mean in Ruby?

When Ruby executes the def keyword, it simply redefines it (whether the method already exists or not). This is called overriding. See the following example : def language puts("We are learning PHP") end def language puts("We are learning Ruby") end # Now call the method language.

Do you know what a at the end of a method name means Ruby?

In Ruby the ? means that the method is going to return a boolean and the ! modifies the object it was called on. They are there to improve readability when looking at the code.

What is Def and end in Ruby?

The code def hi starts the definition of the method. It tells Ruby that we're defining a method, that its name is hi . The next line is the body of the method, the same line we saw earlier: puts "Hello World" . Finally, the last line end tells Ruby we're done defining the method.


2 Answers

In general, methods that end in ! indicate that the method will modify the object it's called on. Ruby calls these as "dangerous methods" because they change state that someone else might have a reference to. Here's a simple example for strings:

foo = "A STRING"  # a string called foo foo.downcase!     # modifies foo itself puts foo          # prints modified foo 

This will output:

a string 

In the standard libraries, there are a lot of places you'll see pairs of similarly named methods, one with the ! and one without. The ones without are called "safe methods", and they return a copy of the original with changes applied to the copy, with the callee unchanged. Here's the same example without the !:

foo = "A STRING"    # a string called foo bar = foo.downcase  # doesn't modify foo; returns a modified string puts foo            # prints unchanged foo puts bar            # prints newly created bar 

This outputs:

A STRING a string 

Keep in mind this is just a convention, but a lot of Ruby classes follow it. It also helps you keep track of what's getting modified in your code.

like image 89
Todd Gamblin Avatar answered Oct 10 '22 16:10

Todd Gamblin


The exclamation point means many things, and sometimes you can't tell a lot from it other than "this is dangerous, be careful".

As others have said, in standard methods it's often used to indicate a method that causes an object to mutate itself, but not always. Note that many standard methods change their receiver and don't have an exclamation point (pop, shift, clear), and some methods with exclamation points don't change their receiver (exit!). See this article for example.

Other libraries may use it differently. In Rails an exclamation point often means that the method will throw an exception on failure rather than failing silently.

It's a naming convention but many people use it in subtly different ways. In your own code a good rule of thumbs is to use it whenever a method is doing something "dangerous", especially when two methods with the same name exist and one of them is more "dangerous" than the other. "Dangerous" can mean nearly anything though.

like image 39
Brian Carper Avatar answered Oct 10 '22 16:10

Brian Carper