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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-02-19 12:23
What is difference between break statement and return statement?
Cc704ce316e72af824ccd38cfa53104b?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.
This topic is locked and can not be replied to.