Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to prevent returning last evaluated expression

Tags:

ruby

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 a 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
like image 308
Ryan Haining Avatar asked Jul 08 '13 23:07

Ryan Haining


1 Answers

If you want to make it "poke you in the eye" explicit, just say "this method doesn't return anything":

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 an unambiguous way to say "this method doesn't return anything of interest", a trailing nil means that "this method explicitly returns nil" and that's not quite the same as not returning anything of use.

like image 143
mu is too short Avatar answered Sep 22 '22 18:09

mu is too short