What does the expression &Proc.new do in a method?
ruby, ruby-on-rails
Solution
`&` has a special meaning in argument list - when used as prefix for Proc object it passes it as block to method being called. Within method body it's just a binary operator.
Problem
I found usage of `&Proc.new` in the rails sources: ``` # rails/railties/lib/rails/engine.rb def routes @routes ||= ActionDispatch::Routing::RouteSet.new @routes.append(&Proc.new) if block_given? @routes end ``` I don't understand how the expression `&Proc.new` works. I wrote similar code and it failed: ``` def method_name &Proc.new if block_given? end proc = method_name{ puts 'Hello world!' } proc.call ``` I received a syntax error: ``` syntax error, unexpected & &Proc.new if block_given? ``` - What does the expression `&Proc.new` do in a method?