wood burning stoves 2.0*
The moose likes Ruby and the fly likes what exactly does those |thing| mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Ruby
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: 12617

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: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what exactly does those |thing| mean?