`

block, lambda, proc的区别

    博客分类:
  • ruby
阅读更多

The second difference is that lambdas have diminutive returns. What this means is that while a Proc return will stop a method and return the value provided, lambdas will return their value to the method and let the method continue on. 

 

http://www.reactive.io/tips/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/

 

http://blog.neten.de/posts/2013/02/17/ruby-block-proc-and-lambda/

 

https://ruby-china.org/topics/20656

 

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics