我在rails源中找到了& Proc.new的用法:
# rails/railties/lib/rails/engine.rb def routes @routes ||= Actiondispatch::Routing::RouteSet.new @routes.append(&Proc.new) if block_given? @routes end
我不明白表达式和Proc.new是如何工作的.
我写了类似的代码,但失败了:
def method_name &Proc.new if block_given? end proc = method_name{ puts 'Hello World!' } proc.call
我收到了语法错误:
Syntax error,unexpected & &Proc.new if block_given?
>表达式和Proc.new在方法中做了什么?