Forum: Ruby What is difference between break statement and return statement?

1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-19 12:23
What is difference between break statement and return statement?
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
on 2014-02-19 12:27
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
5d2429a6afa780bfffcb42beba029d5a?d=identicon&s=25 Dan (Guest)
on 2014-02-19 12:32
(Received via mailing list)
To summarise this answer

http://stackoverflow.com/a/4601654

Break exits from the innermost loop

Return exits from the function
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-19 16:07
@Rajagopalan: Your answer is useful for me. Thank you.


@Dan: I understand clearly from link. Thank you.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.