Posted By

wackimonki on 02/26/07


Tagged

rails rubyonrails adding question


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

wackimonki
webstic


Totalling All of a Variable


 / Published in: Ruby
 

This will add up all the prices and return it? What does |item| mean exactly?

  1. @items.sum { |item| item.price }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wackimonki on February 26, 2007

A little explanation to how the code works.

The method, sum, calls the block as often as it likes, with arguments. The block does something with the arguments and returns a value to the method.

In this case, .sum method calls the block for each item, and the block returns the price for the item to the .sum method.

You need to login to post a comment.