When do we need to use return


Hello ! Just need some help because I Don't understand the use of Return.
When do we need to use return and when we don't ?

Thank you very much for your help

3/7 Omit Needless Words, what is wrong with my code?

From the point of explicitness, we can always use return without 'penalty' and to make code more readable. However, there are many, for lack of a better word, 'trivial' instances where an implicit return is okay, or even favorable.

Implicit return is only ever the last line of a method. Whatever expression appears there is automatically returned.

Hopefully someone with more intimate knowledge on this topic will chime in and elaborate.

Let's look at a simple example of when NOT to use return:

def is_str(a)
  a.select do |x|
    x.is_a? String

arr = ['one', 1, 'two', 2, 'three', 3, 'four', 4]

str = is_str(arr)
puts str

If we use return in the do loop, it is ignored. If we use it after the loop, what do we return? There is no literal return value. Simply writing return will send back nil.

Not the best example, but one of many we could come up with, given the time and energy. Again, hopefully more examples will surface.