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

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.

主站蜘蛛池模板: 邳州市| 琼中| 广东省| 修文县| 新郑市| 中卫市| 舞钢市| 刚察县| 阜阳市| 通渭县| 宁安市| 连平县| 望都县| 淄博市| 巫山县| 灵台县| 巍山| 儋州市| 托克逊县| 黎川县| 合作市| 和田县| 德令哈市| 二连浩特市| 海口市| 岑溪市| 河源市| 江永县| 大悟县| 兴隆县| 青海省| 泸西县| 上饶市| 威远县| 来安县| 曲麻莱县| 锦屏县| 华容县| 郓城县| 新巴尔虎右旗| 东台市|