Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practice for return values from ruby methods

Tags:

ruby

I find myself doing the following a lot to define return values from ruby methods:

def foo
  val = (some expression)
  val
end

This always seems a bit contrived. What's the best practice here?

like image 691
Kevin Bedell Avatar asked Nov 29 '22 19:11

Kevin Bedell


1 Answers

It is unnecessary to save it to a variable unless (some expression) is heavy and will be called multiple times. In that case you might want to cache it.

I would go with either:

def foo
  (some expression)
end

or for caching:

def foo
  @val ||= (some expression)
end
like image 151
DanneManne Avatar answered Dec 26 '22 00:12

DanneManne