- 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.
推薦閱讀
- 造個小程序:與微信一起干件正經事兒
- Practical DevOps
- MySQL數據庫管理與開發(慕課版)
- Learning AndEngine
- Troubleshooting PostgreSQL
- ADI DSP應用技術集錦
- Go并發編程實戰
- C語言程序設計上機指導與習題解答(第2版)
- Learning Concurrent Programming in Scala
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- Unity 2018 Augmented Reality Projects
- 小程序從0到1:微信全棧工程師一本通
- Android系統下Java編程詳解
- 3D Printing Designs:The Sun Puzzle
- 軟硬件綜合系統軟件需求建模及可靠性綜合試驗、分析、評價技術