官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 巢湖市| 博爱县| 马边| 新乡市| 西安市| 阿合奇县| 大宁县| 双辽市| 恭城| 印江| 冕宁县| 五莲县| 隆德县| 黄陵县| 仁寿县| 湖口县| 连南| 简阳市| 兖州市| 喀喇沁旗| 宜阳县| 和林格尔县| 镇原县| 梁山县| 武穴市| 织金县| 平山县| 南昌市| 永泰县| 仪陇县| 镇雄县| 宜兰县| 门头沟区| 揭西县| 武隆县| 昌都县| 东乡| 长子县| 怀柔区| 于田县| 师宗县|