- Comprehensive Ruby Programming
- Jordan Hudgens
- 141字
- 2021-07-02 21:13:32
Return behavior
Secondly, lambdas and procs have different behaviors when it comes to returning values from methods. To see this, I'm going to create a method called my_method:
def my_method
x = lambda {return}
x.call
p "Text within the method"
end
my_method
If I run this method, it prints out "Text within the method":

Now, let's try exactly the same implementation with a proc:
def my_method
x = Proc.new {return}
x.call
p "Text within the method"
end
my_method
When you run it this time, it returns a value of nil.

What happened is that when the proc saw the return word, it exited out of the entire method and returned a nil value. However, in the case of the lambda, it processed the remaining part of the method.
So, these are the subtle and yet important differences between lambdas and procs.
推薦閱讀
- Android應用程序開發與典型案例
- Java面向對象軟件開發
- 新手學Visual C# 2008程序設計
- SQL Server 2016數據庫應用與開發習題解答與上機指導
- Visual C++應用開發
- SQL Server 2016數據庫應用與開發
- Java系統化項目開發教程
- 新一代SDN:VMware NSX 網絡原理與實踐
- Illustrator CC平面設計實戰從入門到精通(視頻自學全彩版)
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- JavaScript編程精解(原書第2版)
- Web開發的平民英雄:PHP+MySQL
- 原型設計:打造成功產品的實用方法及實踐
- Visual FoxPro程序設計實驗教程
- Flask Web開發實戰:入門、進階與原理解析