51

I have a method where I would like to decide what to return within a map function. I am aware that this can be done with assigning a variable, but this is how I though I could do it;

def some_method(array)
    array.map do |x|
      if x > 10
         return x+1 #or whatever
      else
         return x-1
      end
    end
end

This does not work as I expect because the first time return is hit, it returns from the method, and not in the map function, similar to how the return is used in javascript's map function.

Is there a way to achieve my desired syntax? Or do I need to assign this to a variable, and leave it hanging at the end like this:

def some_method(array)
    array.map do |x|
      returnme = x-1
      if x > 10
         returnme = x+1 #or whatever
      end
      returnme
    end
end
2
  • 1
    Related question with some more answers and details: Using 'return' in a Ruby block. Commented May 23, 2020 at 20:40
  • 1
    return in javascript has the opposite behavior. It does return from the map function and not from any outer method. Sorry, just clarifying cause it sounds like you're saying they behave the same. Commented Apr 16, 2022 at 0:05

3 Answers 3

103

Sergio's answer is very good, but it's worth pointing out that there is a keyword that works the way you wanted return to work: next.

array.map do |x|
  if x > 10
    next x + 1
  else
    next x - 1
  end
end

This isn't a very good use of next because, as Sergio pointed out, you don't need anything there. However, you can use next to express it more explicitly:

array.map do |x|
  next x + 1 if x > 10
  x - 1
end
Sign up to request clarification or add additional context in comments.

2 Comments

That is nice! My actual code is a bit more convoluted than my example above, so next could be a very nice solution. Personally I don't like all this "magic by default" behaviour of ruby. It is nice to explicitly state what is going on so it is easier for the next person to pick it up.
Cool, I have used Ruby a lot but have missed this with next. Some day I will start reading manuals...
31

You don't need the variable. Return value of the block is value of last expression evaluated in it. In this case, the if.

def some_method(array)
    array.map do |x|
      if x > 10
         x+1
      else
         x-1
      end
    end
end

Ternary operator would look nicer, I think. More expression-ish.

def some_method(array)
  array.map do |x|
    (x > 10) ? x+1 : x-1
  end
end

If you insist on using return, then you could use lambdas. In lambdas, return behaves like in normal methods.

def some_method(array)
  logic = ->(x) {
    if x > 10
      return x + 1
    else
      return x - 1
    end
  }
  array.map(&logic)
end

This form is rarely seen, though. If your code is short, it surely can be rewritten as expressions. If your code is long and complicated enough to warrant multiple exit points, then probably you should try simplifying it.

4 Comments

Nice! I had to rework my code a bit, but I believe this is a great solution!
Coming back to this some time later, I am still amazed that implicit return works, but not explicit return. It is so strange.
@Automatico: hopefully you'll get used to this :)
I couldn't. Jumped ship to typescript for most om my projects. @SergioTulentsev
1

The return keyword can only be used within a method(actually including Proc). It will raise the LocalJumpError

irb(main):123:0> array = [1, 2, 3, 4, 5, 8, 9, 10, 11, 14, 15]
irb(main):124:0> array.map { |n| return n }
Traceback (most recent call last):
        3: from (irb):124
        2: from (irb):124:in `map'
        1: from (irb):124:in `block in irb_binding'
LocalJumpError (unexpected return)

You can use next instead of return.

irb(main):126:0> array.map { |n| next n }
=> [1, 2, 3, 4, 5, 8, 9, 10, 11, 14, 15]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.