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
I wanted to edit the approved answer to simplify the example, but my edit was rejected with suggestion of making new answer. So this is my simplified version:
def testing(target, method)
(1..3).each do |x|
(1..3).each do |y|
print x*y
if x*y == target
break if method == "break"
return if method == "return"
end
end
end
end
we can see the difference trying:
testing(3, "break")
testing(3, "return")
Results of first (break statement exiting innermost loop only when 3 reached):
1232463
Results of last (return statement exiting whole function):
123