Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this ampersand mean? [duplicate]

Tags:

ruby

Possible Duplicate:
What does map(&:name) mean in Ruby?
What do you call the &: operator in Ruby?

Just watching some railscast and see code like this:

[Category, Product, Person].each(&:delete_all)

I know that it erases all records for these models, but I can't find out what this &:delete_all means.

like image 983
Marcin Doliwa Avatar asked Jan 16 '23 01:01

Marcin Doliwa


2 Answers

It's basically shorthand for this:

[Category, Product, Person].each { |e| e.delete_all }

That is, it sends delete_all to each element of the iterator.

like image 147
mipadi Avatar answered Jan 23 '23 14:01

mipadi


&:delete_all basically translates to |obj| obj.delete_all. The ampersand calls to_proc on the current object on the loop.

like image 32
alemangui Avatar answered Jan 23 '23 14:01

alemangui