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

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.

主站蜘蛛池模板: 桂平市| 乌兰察布市| 铁岭县| 阿图什市| 乌兰浩特市| 南漳县| 班戈县| 南陵县| 孙吴县| 泰宁县| 遂昌县| 永城市| 阜阳市| 雷山县| 武穴市| 荆门市| 胶南市| 耒阳市| 郧西县| 上饶县| 威海市| 齐齐哈尔市| 和平区| 大方县| 湖口县| 东乡县| 平和县| 宁河县| 乌拉特前旗| 称多县| 阳高县| 嫩江县| 六盘水市| 开江县| 安丘市| 固镇县| 东海县| 绍兴县| 会东县| 资中县| 精河县|