1

For example, I have enum.map(&:join).map(&:to_i). Is there a syntax in Ruby where I can write something like this: enum.map(&:join:to_i) in order to avoid iterating through the array twice using & operator?

1 Answers1

0

You could use a block (with no &) to iterate just once:

enum.map { |e| e.join.to_i }
Gerry
  • 10,337
  • 3
  • 31
  • 40
  • that's what I actually did. Is there anything could be done like that with & operator? –  Jun 11 '17 at 04:16
  • @Mikhail Oh, i see. I am not aware of any, i searched a while ago with no success. Is there a specific reason you need to use `&`? Or is a matter of style? – Gerry Jun 11 '17 at 04:21
  • @Mikhail Check [this post](https://stackoverflow.com/questions/9460204/chaining-methods-using-symbolto-proc-shorthand-in-ruby). – Gerry Jun 11 '17 at 04:24