新手问题 新手问 Block, Proc, Lambda

geektony · July 24, 2014 · Last by flowerwrong replied at July 24, 2014 · 2101 hits

Block, Proc, Lambda

请问有什么资源可以较好地学习和理解这三者

Ruby 元编程。

#3 楼 @Peter lambda 会返回它的调用函数,但 Proc 会结束它所位于的 function

def return_from_proc
  ruby_proc = Proc.new { return "return from a Proc" }
  ruby_proc.call
  return "The function will NOT reach here because a Proc containing a return statement has been called"
end

def return_from_lambda
  ruby_lambda = lambda { return "return from lambda" }
  ruby_lambda.call
  return "The function will reach here"
end

puts return_from_proc # display return from proc
puts return_from_lambda # display The function will reach here

求解释。lambda 会返回它的调用函数,但 Proc 会结束它所位于的 function,这句话和代码怎么对应,我理解的和结果打印刚好相反。

You need to Sign in before reply, if you don't have an account, please Sign up first.