Difference between Procs and Lambdas
23 Mar 2014 Tags: | ruby
It is important to mention that they are both Proc objects.
- Lambdas
check the number of arguments
, while procs do not
In contrast, procs don’t care if they are passed the wrong number of arguments.
As shown above, procs don’t freak out and raise errors if they are passed the wrong number of arguments.
If the proc requires an argument but no argument is passed then the proc returns nil. If too many arguments are passed than it ignores the extra arguments.
- Lambdas and procs treat the
‘return’ keyword differently
‘return’ inside of a lambda triggers the code right outside of the lambda code
‘return’ inside of a proc triggers the code outside of the method where the proc is being executed
Thanks for reading!