1

Is there a keyword I can use to explicitly tell the map function what the result of that particular iteration should be?

Consider:

a = [1,2,3,4,5]
a.map do |element|
  element.to_s
end

In the above example element.to_s is implicitly the result of each iteration.

There are some situations where I don't want to rely on using the last executed line as the result, I would prefer to explicitly say what the result is in code.

For example,

a = [1,2,3,4,5]
a.map do |element|
  if some_condition
    element.to_s
  else
    element.to_f
  end
end

Might be easier for me to read if it was written like:

a = [1,2,3,4,5]
a.map do |element|
  if some_condition
    result_is element.to_s
  else
    result_is element.to_f
  end
end

So is there a keyword I can use in place of result_is?

return will return from the calling function, and break will stop the iteration early, so neither of those is what I'm looking for.

0

4 Answers 4

4

The last thing left on the stack is automatically the result of a block being called. You're correct that return would not have the desired effect here, but overlook another possibility: Declaring a separate function to evaluate the entries.

For example, a reworking of your code:

def function(element)
   if (some_condition)
     return element.to_s
   end

   element.to_f
end

a.map do |element|
  function(element)
end

There is a nominal amount of overhead on calling the function, but on small lists it should not be an issue. If this is highly performance sensitive, you will want to do it the hard way.

Sign up to request clarification or add additional context in comments.

1 Comment

tap just returns whatever object is passed in as an argument regardless of what happens in the block. I'm not sure how that's useful as it doesn't allow for different results.
1

Yes, there is, it's called next. However, using next in this particular case will not improve readability. On the contrary, it will a) confuse the reader and b) give him the impression that the author of that code doesn't understand Ruby.

The fact that everything is an expression in Ruby (there are no statements) and that every expression evaluates to the value of the last sub-expression in that expression are fundamental Ruby knowledge.

Just like return, next should only be used when you want to "return" from the middle of a block. Usually, you only use it as a guard clause.

Comments

1

The nature of map is to assign the last executed line to the array. Your last example is very similar to the following, which follows the expected behavior:

a = [1,2,3,4,5]
a.map do |element|
  result = if some_condition
    element.to_s
  else
    element.to_f
  end
  result
end

1 Comment

why assign the result of the if expression to result and return result if you can just return the result of the if expression directly? :)
0

No, there is no language keyword in ruby you can use to determine the result mapped into the resulting array before executing other code within the iteration.

You may assign a variable which you then return when some other code has been executed:

a.map do |element|
  result = some_condition ? element.to_s : element.to_f

  #do something else with element

  result
end

Keep in mind the reason for ruby not providing a keyword for this kind of code is that these patterns tend to have a really low readability.

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.