Why does Ruby use yield?

后端 未结 4 801
天命终不由人
天命终不由人 2020-12-30 05:14

I am new to Ruby. I have used a lot of C# and JavaScript which allow higher-order functions and I typically use them on a daily basis.

Ruby seems a little strange to

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 05:18

    One advantage of yield is it also lets you use next (like continue) and break. In other languages, for next, you might have to use return, and for break, you might have to (ab)use exceptions. It is arguably nicer to have built-in support for these sorts of operations.

提交回复
热议问题