4

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.

Community
  • 1
  • 1
Marcin Doliwa
  • 3,639
  • 3
  • 37
  • 62
  • 1
    This is a duplicate of no less than 20 other questions that have already been asked and answered here on StackOverflow: [Understanding \[ClassOne, ClassTwo\].each\(&:my_method\)](http://StackOverflow.Com/q/99318/), [What does `map(&:name)` mean in Ruby?](http://StackOverflow.Com/q/1217088/), [What exactly is `&:capitalize` in Ruby?](http://StackOverflow.Com/q/1792683/), [Ruby/Ruby on Rails ampersand colon shortcut](http://StackOverflow.Com/q/1961030/), [Ruby : `&:symbol` syntax](http://StackOverflow.Com/q/2096975/), … – Jörg W Mittag Nov 14 '12 at 17:13
  • 1
    … [What is this `&:last` Ruby Construct Called?](http://StackOverflow.Com/q/2211751/), [What do you call the `&:` operator in Ruby?](http://StackOverflow.Com/q/2259775/), [What does `map(&:name)` do in this Ruby code?](http://StackOverflow.Com/q/2388337/), [What are `:+` and `&+` in ruby?](http://StackOverflow.Com/q/2697024/), [`&:views_count` in `Post.published.collect(&:views_count)`](http://StackOverflow.Com/q/3888044/), [Ruby Proc Syntax](http://StackOverflow.Com/q/4512587/), [How does “`(1..4).inject(&:+)`” work in Ruby](http://StackOverflow.Com/q/5003257/), … – Jörg W Mittag Nov 14 '12 at 17:13
  • 1
    … [Ruby map method syntax question](http://StackOverflow.Com/q/5231919/), [What does following statement `&:property` ?](http://StackOverflow.Com/q/5620411/), [What does the `&` mean in the following ruby syntax?](http://StackOverflow.Com/q/5952175/), [Why would one use the unary operator on a property in ruby? i.e `&:first`](http://StackOverflow.Com/q/6289084/), [how does `Array#map` have parameter to do something like this?](http://StackOverflow.Com/q/6716629/), [what does `&:` mean in ruby, is it a block mixed with a symbol?](http://StackOverflow.Com/q/9188362/), … – Jörg W Mittag Nov 14 '12 at 17:14
  • 1
    … [what is the functionality of “`&:`” operator in ruby?](http://StackOverflow.Com/q/9429819/), and [What does “`temps.each(&:valid?)`” mean in Ruby?](http://StackOverflow.Com/q/9926724/). – Jörg W Mittag Nov 14 '12 at 17:14

3 Answers3

7

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.

mipadi
  • 398,885
  • 90
  • 523
  • 479
1

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

alemangui
  • 3,571
  • 22
  • 33
1

When you put a Proc object pr with & in the last argument position such as in:

some_method(&pr)

then, a block corresponding to pr will be passed to some_method. If an object non_pr that is not a Proc is given like:

some_method(&non_pr)

then, non_pr will be implicitly converted to a Proc by to_proc.

For example, when non_pr is a Symbol, then Symbol#to_proc will be applied, which happens to be something like this:

class Symbol
  def to_proc
    proc{|obj, *args| obj.send(self, *args)}
  end
end

Particularly with each(&:delete_all), the :delete_all.to_proc will return the Proc object:

proc{|obj, *args| obj.delete_all(*args)}

so the corresponding block will be passed to each like this:

each{|obj, *args| obj.delete_all(*args)}

Noticing that the arity of a block for Enumerable#each is one, this is simplified to:

each{|obj| obj.delete_all}
sawa
  • 165,429
  • 45
  • 277
  • 381