- Comprehensive Ruby Programming
- Jordan Hudgens
- 135字
- 2021-07-02 21:13:32
Argument count
The first key difference is that lambdas count the arguments you pass to them, whereas procs do not. Consider this example:
full_name = lambda { |first, last| first + " " + last}
p full_name.call("Jordan", "Hudgens")
Running this code will work properly. However, observe it when I pass another argument like this:
p full_name.call("Jordan", "David", "Hudgens")
The application throws an error saying that we're passing in the wrong number of arguments:

Now, let's see what happens with procs:
full_name = Proc.new{ |first, last| first + " " + last}
p full_name.call("Jordan", "David", "Hudgens")
If you run this code, you can see that it does not throw an error. It simply looks at the first two arguments and ignores anything after that.

In review, lambdas count the arguments passed to them, whereas procs don't.
推薦閱讀
- Getting Started with ResearchKit
- JIRA 7 Administration Cookbook(Second Edition)
- TestNG Beginner's Guide
- Git高手之路
- 深度學(xué)習(xí):算法入門與Keras編程實(shí)踐
- 3D少兒游戲編程(原書第2版)
- Visual C++數(shù)字圖像處理技術(shù)詳解
- JavaScript入門經(jīng)典
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- Kubernetes進(jìn)階實(shí)戰(zhàn)
- Python自然語言理解:自然語言理解系統(tǒng)開發(fā)與應(yīng)用實(shí)戰(zhàn)
- Sails.js Essentials
- 零基礎(chǔ)學(xué)Java第2版
- Java程序設(shè)計(jì)教程
- Docker on Windows