- 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.
推薦閱讀
- 樂(lè)學(xué)Web編程:網(wǎng)站制作不神秘
- 實(shí)戰(zhàn)Java程序設(shè)計(jì)
- 一塊面包板玩轉(zhuǎn)Arduino編程
- Kivy Cookbook
- 軟件測(cè)試綜合技術(shù)
- Data Science Algorithms in a Week
- Unity 2017 Game AI Programming(Third Edition)
- Java EE 7 with GlassFish 4 Application Server
- Mastering HTML5 Forms
- Arduino機(jī)器人系統(tǒng)設(shè)計(jì)及開發(fā)
- Android Studio開發(fā)實(shí)戰(zhàn):從零基礎(chǔ)到App上線 (移動(dòng)開發(fā)叢書)
- Applied Deep Learning with Python
- IBM RUP參考與認(rèn)證指南
- 深入理解Kafka:核心設(shè)計(jì)與實(shí)踐原理
- HTML5 Canvas核心技術(shù):圖形、動(dòng)畫與游戲開發(fā)