break and return in ruby, how do you use them?

后端 未结 4 1716
猫巷女王i
猫巷女王i 2020-12-23 11:52

I just asked a question about return and it seems to do the same thing as break. How do you use return, and how do you use break, such as in the actual code that you write t

4条回答
  •  -上瘾入骨i
    2020-12-23 12:40

    One more example could be if you have two loops in a single method and if you want to come out of the first loop and continue to the second loop use break in the first loop or vice versa:

    def testing(method)
    x=1
      10.times do
        if(x == 2)
         break if method == "break"
        end
       x+=1
      end
      10.times do
       if(x == 5)
         return if method == "return"
       end
      x+=1
      end 
    end
    

提交回复
热议问题