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

Return behavior

Secondly, lambdas and procs have different behaviors when it comes to returning values from methods. To see this, I'm going to create a method called my_method:

def my_method 
x = lambda {return}
x.call
p "Text within the method"
end

my_method

If I run this method, it prints out "Text within the method":

Now, let's try exactly the same implementation with a proc:

def my_method 
x = Proc.new {return}
x.call
p "Text within the method"
end

my_method

When you run it this time, it returns a value of nil.

What happened is that when the proc saw the return word, it exited out of the entire method and returned a nil value. However, in the case of the lambda, it processed the remaining part of the method.

So, these are the subtle and yet important differences between lambdas and procs.

主站蜘蛛池模板: 阜平县| 万年县| 安顺市| 东安县| 丰原市| 大悟县| 利川市| 阆中市| 荔浦县| 金阳县| 会宁县| 博野县| 淄博市| 武宁县| 潜江市| 望江县| 麻阳| 临清市| 南平市| 四子王旗| 隆林| 苏州市| 略阳县| 新泰市| 渭源县| 沙雅县| 乌鲁木齐县| 长岛县| 洛隆县| 贡觉县| 昌乐县| 大安市| 霍山县| 万载县| 安多县| 兴山县| 大庆市| 灵璧县| 潮安县| 宁蒗| 济宁市|