Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what exactly does those |thing| mean?

 
Breno Salgado
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic