Quantcast
Channel: `return` in Ruby Array#map - Stack Overflow
Browsing all 4 articles
Browse latest View live

Answer by TorvaldsDB for `return` in Ruby Array#map

The return keyword can only be used within a method(actually including Proc). It will raise the LocalJumpErrorirb(main):123:0> array = [1, 2, 3, 4, 5, 8, 9, 10, 11, 14, 15]irb(main):124:0>...

View Article



Answer by Jordan Running for `return` in Ruby Array#map

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 endendThis...

View Article

Answer by Sergio Tulentsev for `return` in Ruby Array#map

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...

View Article

`return` in Ruby Array#map

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...

View Article
Browsing all 4 articles
Browse latest View live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>