Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

what exactly does those |thing| mean?

 
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?
 
Author
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
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.
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic