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

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.

主站蜘蛛池模板: 德阳市| 开封市| 出国| 温州市| 大厂| 东兴市| 杭锦旗| 潮州市| 长宁区| 绩溪县| 苗栗市| 溧阳市| 区。| 五原县| 澄城县| 新河县| 藁城市| 英山县| 房产| 金阳县| 报价| 泾川县| 子洲县| 塘沽区| 遵义市| 名山县| 陕西省| 金平| 方山县| 临西县| 榆中县| 无锡市| 东城区| 长垣县| 旌德县| 南皮县| 安平县| 抚松县| 永德县| 高雄县| 启东市|