# Can someone please explain this line of code?

**URL:** <https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737>\
**Category:** Rails\
**Created:** [November 30, 2006, 3:26am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737 "2006-11-30T03:26:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 3:26am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/1 "2006-11-30T03:26:19Z")

</div>

> From Agile Rails Development:

current\_item = @items.find {|item| item.product == product}

it’s at the start of the “add\_product” method in the model “cart”

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 3:46am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/2 "2006-11-30T03:46:20Z")

</div>

On Nov 29, 2006, at 8:25 PM, Brandon wrote:

> current\_item = @items.find {|item| item.product == product}
> 
> it’s at the start of the “add\_product” method in the model “cart”

It finds an entry in the @items list whose product attribute equals  
the parameter product. If none exists, it sets current\_item to nil.

Dave

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 4:20am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/3 "2006-11-30T04:20:25Z")

</div>

Thank you!

The book rocks by the way!

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 4:32am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/4 "2006-11-30T04:32:27Z")

</div>

On Nov 29, 2006, at 9:05 PM, Bill wrote:

> Do I detect that Dave has some free time now that the book has gone to  
> the publisher? Wait, I think I hear Rails 2.0 calling…

Get thee behind me, Satan…

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 4:34am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/5 "2006-11-30T04:34:19Z")

</div>

Brandon Wright wrote:

> > From Agile Rails Development:
> 
> current\_item = @items.find {|item| item.product == product}
> 
> it’s at the start of the “add\_product” method in the model “cart”

I suppose it takes a pair of brass ones to reply to this right after  
somebody who claims to be Dave T., but I think I can add to his  
answer. It is a bit ironic that Dave answered this, since Brandon’s  
lack of understanding is almost certainly due to not having read Dave’s  
book on Ruby.

Brandon, you should do yourself a favor and read Dave’s “PickAxe” book  
called Programming Ruby. It will make learning Rails much easier.

Meanwhile, to get you through this one, let me just add that this  
probably looks “wierd” to you because you are not familiar yet with a  
common Ruby thing called an “iterator”. It is basically a method  
(function) that makes it ridiculously easy to step through all of the  
items in a list (or in this case a table of a database), and do  
something with each item. It’s sort of a for loop without all the  
extraneous nonsense. The object knows how to iterate over it’s items,  
all you have to supply is the little block of code that says what to do  
with each one.

So here, the iterator “find” (which “items” inherits from the  
ActiveRecord class that all Rails models belong to) feeds all of the  
“items” in the database to the block of code in the braces one at a time  
and lets that block compare the product attribute of the item to the  
“product” parameter that was passed in. The block just looks for an  
item where the product field equals the one we are looking for. The  
block of code returns true or false for each “iteration”, and when it  
returns true, the “find” method says "oh, that’s the one you were  
looking for, and returns it. If it doesn’t ever see a “true” from the  
block, and it runs out of items to iterate over, it gives up and returns  
nil.

The unusual looking |item| construct at the beginning of the code block  
is just how Ruby allows the programmer to say “for each one of the  
things you’re about to pass me, I’m going to call them ‘item’ in this  
code block”. The code block could just as easily been {|Dave|  
Dave.product == product} and it would work the same way. Using “item”  
just makes more sense.

How’d I do Dave?

jp

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 5:00am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/6 "2006-11-30T05:00:31Z")

</div>

Hi –

On Thu, 30 Nov 2006, Jeff P. wrote:

> The unusual looking |item| construct at the beginning of the code block  
> is just how Ruby allows the programmer to say “for each one of the  
> things you’re about to pass me, I’m going to call them ‘item’ in this  
> code block”.

It also has implications if you already have a variable called item –  
namely, it will assign to _that_ variable. (In Ruby 1.8 and earlier,  
that is.) That can be advantageous (which is why I’m sad that it’s  
disappearing), but also means one has to be careful in choosing a  
variable name since it could have an effect outside the block.

> The code block could just as easily been {|Dave|  
> Dave.product == product} and it would work the same way. Using “item”  
> just makes more sense.

You wouldn’t want to use a constant there, though. It will run, but  
you’ll get warnings.

> How’d I do Dave?

Whoops, I’m the wrong Dav(e|id). I’m just pinch-hitting 🙂

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:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 6:36am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/7 "2006-11-30T06:36:48Z")

</div>

Thanks for taking the time to write that reply - Unfortunately I can’t  
read that many books all at once, although I’d point out that Dave  
Thomas is co-author (with DHH) of the book I am going through (Agile  
Web D. with Rails 2nd Ed.). I’ll get to “Programming Ruby”  
soon enough, I’m pretty much addicted to how cool this framework is.

I think it’s great that everyone is so involved in the community that a  
newbie like me posts a rather basic question, and gets replys from two  
very notable Dave’s (Thomas and Black) and a very well thought out  
response from you.

Thanks everyone!!

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 6:36am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/8 "2006-11-30T06:36:49Z")

</div>

Thanks for taking the time to write that reply - Unfortunately I can’t  
read that many books all at once, although I’d point out that Dave  
Thomas is co-author (with DHH) of the book I am going through (Agile  
Web D. with Rails 2nd Ed.). I’ll get to “Programming Ruby”  
soon enough, I’m pretty much addicted to how cool this framework is.

I think it’s great that everyone is so involved in the community that a  
newbie like me posts a rather basic question, and gets replys from two  
very notable Dave’s (Thomas and Black) and a very well thought out  
response from you.

Thanks everyone!!

---

<div class="post-metadata">

**Author:** ![brndn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dec6dc/32.png) [@brndn](https://www.ruby-forum.com/u/brndn)\
**Post date:** [November 30, 2006, 4:05am UTC](https://www.ruby-forum.com/t/can-someone-please-explain-this-line-of-code/77737/9 "2006-11-30T04:05:57Z")

</div>

Dave T. wrote:

> On Nov 29, 2006, at 8:25 PM, Brandon wrote:
> 
> > current\_item = @items.find {|item| item.product == product}
> > 
> > it’s at the start of the “add\_product” method in the model “cart”
> 
> It finds an entry in the @items list whose product attribute equals  
> the parameter product. If none exists, it sets current\_item to nil.
> 
> Dave

Do I detect that Dave has some free time now that the book has gone to  
the publisher? Wait, I think I hear Rails 2.0 calling…
