What is the return point in Ruby?

What is the difference between return and just putting a variable, for example:

return is not refundable

 def write_code(number_of_errors) if number_of_errors > 1 mood = "Ask me later" else mood = "No Problem" end mood end 

return

 def write_code(number_of_errors) if number_of_errors > 1 mood = "Ask me later" else mood = puts "No Problem" end return mood end 
+76
ruby return
Jan 05
source share
7 answers

return allows you to break through early:

 def write_code(number_of_errors) return "No problem" if number_of_errors == 0 badness = compute_badness(number_of_errors) "WHAT?! Badness = #{badness}." end 

If number_of_errors == 0 , then "No problem" will be returned immediately. However, at the end of the method, this is not necessary, as you noticed.




Edit: To demonstrate that return exits immediately, consider this function:

 def last_name(name) return nil unless name name.split(/\s+/)[-1] end 

If you call this function as last_name("Antal SZ") , it will return "SZ" . If you call it last_name(nil) , it returns nil . If return not interrupted immediately, it will try to execute nil.split(/\s+/)[-1] , which will cause an error.

+112
Jan 05 2018-11-11T00:
source share

Using "return" is not required if this is the last line to be executed in the method, since Ruby will automatically return the last evaluated expression.

You don’t even need this final β€œmood”, and you don’t need these assignments in the IF statement.

 def write_code(number_of_errors) if number_of_errors > 1 "ERROR" else "No Problem" end end puts write_code(10) 

Exit:

MISTAKE

+38
Jan 05 2018-11-11T00
source share

I use return when I scroll through the list, and I want to exit the function if any member of the list matches the criteria. I could accomplish this with a single statement, for example:

list.select{|k| k.meets_criteria}.length == 0

in some situations but

list.each{|k| return false if k.meets_criteria}

- also one line - in my opinion, added flexibility. For example, the first example assumes that this is the only line in the method and that we want to return from this point no matter what. But if it's a test to make sure it's safe to continue working with the rest of the method, the first example will have to handle it differently.

EDIT:

To add some flexibility, consider the following line of code:

list_of_method_names_as_symbols.each{|k| list_of_objects.each{|j| return k if j.send(k)}}

I am sure that this can be achieved in one line, without return , but from the top of my head I do not see how to do this.

But now this is a rather flexible line of code that can be called by any list of logical methods and a list of objects that implement these methods.

EDIT

It should be noted that I assume that this line is inside the method, not the block.




But this is basically a stylistic choice, I think that in most situations, you can and possibly avoid using return .

+4
Jan 05 2018-11-11T00:
source share

Ruby is always back! The best way -

 def write_code(number_of_errors) (number_of_errors > 1)? "ERROR" : "No Problem" end 

this means that if number_of_errors> 1, it will return ERROR else No Problem

+4
Jul 27 '13 at 15:11
source share

A good ruby ​​gives this good opportunity not to specify the return statement explicitly, but I just feel that as standard programming you should always strive to specify "return" statements wherever needed. This helps to make the code more readable for those who come from different sources, such as C ++, Java, PHP, etc., and study ruby. The "return" operation does no harm, so why skip the normal and more standard way of returning from functions.

+3
Dec 09 '13 at 4:27
source share

One small warning for those coming from other languages. Suppose you have a function like OP, and you use the "last thing calculated" rule to automatically set the return value:

 def write_code(number_of_errors) if number_of_errors > 1 mood = "Ask me later" else mood = "No Problem" end end 

and suppose you add a debug statement (or a log entry):

 def write_code(number_of_errors) if number_of_errors > 1 mood = "Ask me later" else mood = "No Problem" end puts "### mood = #{mood}" end 

Now guess what. You violated your code because puts returns nil, which now becomes the return value from the function.

The solution is to get used to always explicitly specify the return value in the last line, as the OP did:

 def write_code(number_of_errors) if number_of_errors > 1 mood = "Ask me later" else mood = "No Problem" end puts "### mood = #{mood}" mood end 
0
Aug 12 '19 at 18:07
source share

Unnecesarity return on the last line in a function is just Ruby syntactic sugar. In most procedural languages, you need to write return in each (non-void in C ++) function.

-one
Jan 05 2018-11-11T00:
source share



All Articles