- 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.
推薦閱讀
- C語言程序設(shè)計(jì)案例教程
- Android Wearable Programming
- Mastering Concurrency Programming with Java 8
- MySQL數(shù)據(jù)庫應(yīng)用與管理 第2版
- Python從小白到大牛
- Django:Web Development with Python
- JS全書:JavaScript Web前端開發(fā)指南
- iOS編程基礎(chǔ):Swift、Xcode和Cocoa入門指南
- Creating Data Stories with Tableau Public
- 機(jī)器學(xué)習(xí)微積分一本通(Python版)
- Zabbix Performance Tuning
- C++程序設(shè)計(jì)教程
- Keil Cx51 V7.0單片機(jī)高級語言編程與μVision2應(yīng)用實(shí)踐
- INSTANT LESS CSS Preprocessor How-to
- 大話代碼架構(gòu):項(xiàng)目實(shí)戰(zhàn)版