- 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.
推薦閱讀
- R語言數據分析從入門到精通
- Reporting with Visual Studio and Crystal Reports
- Learning RabbitMQ
- C/C++常用算法手冊(第3版)
- Python編程實戰
- 大模型RAG實戰:RAG原理、應用與系統構建
- HTML5與CSS3基礎教程(第8版)
- Integrating Facebook iOS SDK with Your Application
- Learning Apache Karaf
- D3.js By Example
- 軟件供應鏈安全:源代碼缺陷實例剖析
- CodeIgniter Web Application Blueprints
- JavaScript悟道
- HTML5移動Web開發
- Kotlin進階實戰