# Get a list of product ids from a form, then # sum the total price product_list = params[:product_ids] total = Product.find(product_list).sum(&:price) I understand the first list line and part of the second line, but what does the & symbol mean? TIA.
Bala Paranj wrote:> # Get a list of product ids from a form, then > # sum the total price > product_list = params[:product_ids] > total = Product.find(product_list).sum(&:price) > > I understand the first list line and part of the second line, but what > does the & symbol mean?That converts the symbol into a block that sends the symbol to its argument. Full explanation here: http://blog.hasmanythrough.com/articles/2006/03/07/symbol-to-proc-shorthand -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
sum(&:price) is effectively the same as sum { |p| p.price }, just shorter.
http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/core_ext/symbol.rb
-Jonathan.
On 7/29/06, Bala Paranj <bparanj@yahoo.com> wrote:> # Get a list of product ids from a form, then
> # sum the total price
> product_list = params[:product_ids]
> total = Product.find(product_list).sum(&:price)
>
> I understand the first list line and part of the second line, but what does
the & symbol mean?
> TIA.
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>