What is difference between break statement and return statement?

What is difference between break statement and return statement?

hi,

Break statement terminate the loop as below

(1ā€¦10).each do |i|
puts i
break if i.eql?5
end

This loop will be terminated when ā€˜iā€™ reach 5.

but return will return from the function to the caller function like

def hi
a=hello
puts a #will be printed 12
end

def hello
return 12
end

hi

To summarise this answer

http://stackoverflow.com/a/4601654

Break exits from the innermost loop

Return exits from the function

@Rajagopalan: Your answer is useful for me. Thank you.

@Dan: I understand clearly from link. Thank you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs