- 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.
推薦閱讀
- Spring Cloud Alibaba核心技術(shù)與實(shí)戰(zhàn)案例
- Python編程自學(xué)手冊
- Python量化投資指南:基礎(chǔ)、數(shù)據(jù)與實(shí)戰(zhàn)
- ASP.NET Core 5.0開發(fā)入門與實(shí)戰(zhàn)
- R語言游戲數(shù)據(jù)分析與挖掘
- UI智能化與前端智能化:工程技術(shù)、實(shí)現(xiàn)方法與編程思想
- Functional Programming in JavaScript
- 老“碼”識途
- MySQL數(shù)據(jù)庫管理與開發(fā)(慕課版)
- 單片機(jī)C語言程序設(shè)計實(shí)訓(xùn)100例
- Instant Nancy Web Development
- INSTANT Adobe Edge Inspect Starter
- PHP與MySQL權(quán)威指南
- 深入淺出Python數(shù)據(jù)分析
- XML程序設(shè)計(第二版)