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

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.

主站蜘蛛池模板: 六枝特区| 大冶市| 仁化县| 明水县| 清水河县| 绥滨县| 本溪| 桑日县| 新乡县| 河间市| 剑河县| 中牟县| 临西县| 乌恰县| 江北区| 屏山县| 平舆县| 灵川县| 天水市| 抚顺县| 武城县| 镇远县| 乌兰县| 沙雅县| 满洲里市| 昌图县| 嵊州市| 云和县| 九台市| 来凤县| 花垣县| 清新县| 南郑县| 成安县| 正蓝旗| 托里县| 杂多县| 汶川县| 盐边县| 浠水县| 佳木斯市|