Best way to prevent the return of the last evaluated expression

Suppose I want to write a method in ruby ​​whose last line is a method call, but I do not want to return its return value. Is there a more elegant way to accomplish this other than adding nil after the call?

 def f(param) # some extra logic with param g(param) # I don't want to return the return of g end 
+4
source share
5 answers

If you want to make it “poke you in the eye”, just say “this method returns nothing”:

 def f(param) # some extra logic with param g(param) # I don't want to return the return of g return end 

f(x) will still evaluate to nil , but a bare return is a definite way of saying that “this method returns nothing interesting”, trailing nil means that “this method explicitly returns nil ”, and it's not exactly the same that do not return anything useful.

+4
source

No, but if it is important that f really returns nil , and not all g(param) returns, then nothing is more elegant than writing with nil in the last line. Why would you do this? In most cases, elegance is clear and obvious.

Several tenants from Zen of Python come to mind:

Explicit is better than implicit.
Simple is better than complex.
Readability indicators.

+6
source

No. If you want to return nil, the last expression must evaluate to nil. You can do this with the nil trailing line or by surrounding the body of the method in nil.tap {} or you like it nonetheless, but it's pretty simple - the last expression is returned.

+5
source

As others have said, no. However, if you want to avoid adding another line, you have several options:

 g(param); nil g(param) && nil 

The first will always call f to return nil; the second returns false (if g returns false) or nil (if g returns the true value).

+4
source

No, there is no other way than to either explicitly return nil or evaluate another expression that implicitly evaluates the value of nil (for example, () ).

If you want to add some kind of semantic marker that shows that you explicitly want to ignore the return value, you can come up with some kind of convention for it, for example:

 def f(param) # some extra logic with param g(param) # I don't want to return the return of g () end 

or

 def f(param) # some extra logic with param g(param) # I don't want to return the return of g _=_ end 

which will make these cases easy to grep capable of, but will probably help little in understanding.

This is the Ruby design choice that it shares with many other expression-based languages: the value of the block / subroutine / procedure / function / method is the value of the last expression evaluated inside the block. So how does it work in Lisp, for example.

Please note that there are other options. For instance. in Smalltalk, the return value of the method must be explicitly returned using the ↑ operator, otherwise the return value is nil . In E, which is largely security-oriented, this is even a conscious design choice: automatically returning the value of the last expression is considered a potential information leak.

0
source

Source: https://habr.com/ru/post/1490386/


All Articles