aspose file tools
The moose likes Ruby and the fly likes what exactly does those |thing| mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Other Languages » Ruby
Reply Bookmark "what exactly does those |thing| mean?" Watch "what exactly does those |thing| mean?" New topic
Author

what exactly does those |thing| mean?

Breno Salgado
Greenhorn

Joined: Nov 05, 2008
Posts: 11
Im learning RoR with pragmatic's Agile Web Development with Rails(3rd edition)

I think Im doing fine til' now, still, I havent seem to understand what exactly does the "|anything|" mean

for example:
current_item = @items.find { |item| item.product == product }

I understand that this line will look into all items, check if the product in each item is the same as the 'product' local variable and probably will set a boolean in current_item for the result(is that right?)

so, does the |item| part means the temporary name of each object?


Breno Santos Salgado -> Brasil, Brasilia, DF.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12612
Breno Salgado wrote:
current_item = @items.find { |item| item.product == product }

I understand that this line will look into all items, check if the product in each item is the same as the 'product' local variable and probably will set a boolean in current_item for the result(is that right?)

Not; it will set current_item to the item where item.product == product.

This depends on the type of @items; it might also return a collection of all items where item.product == product.

so, does the |item| part means the temporary name of each object?

Yes. Basically the find method takes a block (closure). The variable between the pipes/vertical bars is each object during the iteration. This block compares product with each item's product.

You'd be well-served to make sure you understand some Ruby basics before moving much further--otherwise RoR is indistinguishable from magic.
Breno Salgado
Greenhorn

Joined: Nov 05, 2008
Posts: 11
David Newton wrote:
Breno Salgado wrote:
current_item = @items.find { |item| item.product == product }

I understand that this line will look into all items, check if the product in each item is the same as the 'product' local variable and probably will set a boolean in current_item for the result(is that right?)

Not; it will set current_item to the item where item.product == product.

This depends on the type of @items; it might also return a collection of all items where item.product == product.

so, does the |item| part means the temporary name of each object?

Yes. Basically the find method takes a block (closure). The variable between the pipes/vertical bars is each object during the iteration. This block compares product with each item's product.

You'd be well-served to make sure you understand some Ruby basics before moving much further--otherwise RoR is indistinguishable from magic.


Thanks friend

yeah I understand that, well, I must admit Im not THAT good of a learner(as of going deep into ruby before rails ), I just read the ruby language basics appendix and I'm catching up to the language as the examples go... I hope it doesnt hold me much when I start on my own projects... heheh there's always the internet, anyway moose saloon rocks...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12612
For the basics, a general understanding of Ruby is enough. When things go wrong, or you want some more advanced functionality, a good grounding in Ruby is essential.
 
 
subject: what exactly does those |thing| mean?
 
Threads others viewed
Equipment?
Must explicitly qualify the allocation ...
Difference between XP and RUP
Problem Compiling JSP with IIS 4.0 and JRun 2.3
XSD parsing errors
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com