# Newbie question: how to read the "!"

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78048>\
**Category:** Rails\
**Created:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78048 "2006-12-03T00:06:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78048/1 "2006-12-03T00:06:53Z")

</div>

I’m a total beginner at Ruby. I’ve more experience with PHP. I’m trying  
to figure out how to read this method:

def empty\_cart  
@cart = find\_cart  
@cart.empty!  
@items = @cart.items  
redirect\_to(:action =\> ‘index’)  
end

What does

@cart.empty!

mean?

And does this:

```
redirect_to(:action => 'index')

```

mean simply that the action index should be called?

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 1:43am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78048/2 "2006-12-03T01:43:33Z")

</div>

On 22/11/2006, at 10:24 AM, Jake Barnes wrote:

> What does
> 
> @cart.empty!
> 
> mean?

Simple. It calls the method called ‘empty!’ on the @cart object.

It’s a convention in ruby that methods that modify an object in place  
end in ‘!’ and methods that return a boolean end in ‘?’.

> And does this:
> 
> redirect\_to(:action =\> ‘index’)

No. This sends a 302 redirect to the browser so that it then requests  
the index action.

Cheers,

Pete Y.

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 2:01am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78048/3 "2006-12-03T02:01:34Z")

</div>

Hi –

On Sun, 3 Dec 2006, Pete Y. wrote:

> Simple. It calls the method called ‘empty!’ on the @cart object.
> 
> It’s a convention in ruby that methods that modify an object in place  
> end in ‘!’ and methods that return a boolean end in ‘?’.

That’s sort of a convention-within-a-convention. The basic convention  
is that !-methods are “dangerous”, compared to their non-dangerous  
counterparts. The “danger” can, but doesn’t have to, take the form of  
modifying the receiver.

There are also lots of methods that change their receivers but don’t  
end in !, such as Array#pop, Hash#delete, etc.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78048/4 "2006-12-03T19:59:17Z")

</div>

On Nov 22, 2006, at 00:24 , Jake Barnes wrote:

> What does
> 
> @cart.empty!
> 
> mean?

Basically, it calls the empty! method on @cart object. Without  
knowing what class @cart is, it’s impossible to say anything about  
what it actually does. However, judging by the name, I would guess it  
empties the cart. The exclamation mark is a naming convention that  
indicates that the object itself is being modified (as opposed to  
returning a modified copy of the object).

> And does this:
> 
> redirect\_to(:action =\> ‘index’)
> 
> mean simply that the action index should be called?

Pretty much, but not exactly. It adds a redirect header to the  
response, indicating that the connected client should initiate a new  
request, this time for the url identified by :action =\> ‘index’.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)
