- 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.
推薦閱讀
- 多媒體CAI課件設(shè)計與制作導(dǎo)論(第二版)
- Node.js 10實戰(zhàn)
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- C語言程序設(shè)計(第3版)
- 高效微控制器C語言編程
- Practical Internet of Things Security
- 架構(gòu)不再難(全5冊)
- Clojure for Domain:specific Languages
- 64位匯編語言的編程藝術(shù)
- 老“碼”識途
- Hands-On RESTful Web Services with Go
- Swift細(xì)致入門與最佳實踐
- 領(lǐng)域驅(qū)動設(shè)計:軟件核心復(fù)雜性應(yīng)對之道(修訂版)
- Training Systems Using Python Statistical Modeling
- 數(shù)據(jù)科學(xué)中的實用統(tǒng)計學(xué)(第2版)