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”
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”
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
Thank you!
The book rocks by the way!
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…
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
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 | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
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!!
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!!
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…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs