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

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.

主站蜘蛛池模板: 晋州市| 临武县| 阜平县| 千阳县| 临沭县| 宜昌市| 同江市| 甘洛县| 孝昌县| 措勤县| 望奎县| 威远县| 金山区| 浦北县| 湘西| 新蔡县| 晋中市| 方城县| 黄龙县| 宣城市| 衡山县| 陆良县| 南丰县| 大姚县| 嘉鱼县| 夹江县| 宜章县| 乃东县| 大荔县| 泸州市| 彰化县| 桃园县| 正宁县| 咸宁市| 旅游| 广平县| 永登县| 天全县| 西峡县| 苏尼特右旗| 祁连县|